1
1
// noinspection AngularUndefinedBinding
2
2
3
3
import * as nodePath from 'node:path' ;
4
- import { readFile } from 'node:fs/promises' ;
4
+ import { readFile } from 'node:fs/promises' ;
5
5
6
- import { replaceInFile , ReplaceInFileConfig } from 'replace-in-file' ;
7
- import { glob } from 'glob' ;
6
+ import { replaceInFile , ReplaceInFileConfig } from 'replace-in-file' ;
7
+ import { glob } from 'glob' ;
8
8
9
- import { PIPE_IN_BINDING_REGEX , PIPE_REGEX , run } from '../migrate/ngx-translate-migration' ;
9
+ import {
10
+ PIPE_IN_BINDING_REGEX ,
11
+ PIPE_REGEX ,
12
+ run ,
13
+ } from '../migrate/ngx-translate-migration' ;
10
14
11
15
jest . mock ( 'replace-in-file' ) ;
12
16
13
17
describe ( 'ngx-translate migration' , ( ) => {
14
-
15
18
describe ( 'Positive regex tests' , ( ) => {
16
-
17
19
describe ( 'Pipe in binding' , ( ) => {
18
20
test . each ( [
19
21
{
20
22
testCase : `<component [header]="'hello.world' | translate">` ,
21
- match : [ `]="'hello.world' | translate"` ]
23
+ match : [ `]="'hello.world' | translate"` ] ,
22
24
} ,
23
25
{
24
26
testCase : `<component [header]="'hello.world' | translate | anotherPipe">` ,
25
- match : [ `]="'hello.world' | translate | anotherPipe"` ]
27
+ match : [ `]="'hello.world' | translate | anotherPipe"` ] ,
26
28
} ,
27
29
{
28
30
testCase : `<component [header]="'hello' | translate:params | anotherPipe">` ,
29
- match : [ `]="'hello' | translate:params | anotherPipe"` ]
31
+ match : [ `]="'hello' | translate:params | anotherPipe"` ] ,
30
32
} ,
31
33
{
32
34
testCase : `<component [title]="titleMap[reportType] | translate">` ,
33
- match : [ `]="titleMap[reportType] | translate"` ]
35
+ match : [ `]="titleMap[reportType] | translate"` ] ,
34
36
} ,
35
37
{
36
38
testCase : `<component [matTooltip]="('foo.bar' | translate) + ': ' + (value | number: '1.0-2')">` ,
37
- match : [ `]="('foo.bar' | translate) + ': ' + (value | number: '1.0-2')"` ]
39
+ match : [
40
+ `]="('foo.bar' | translate) + ': ' + (value | number: '1.0-2')"` ,
41
+ ] ,
38
42
} ,
39
43
{
40
44
testCase : `<compnent [title]="'Hello, ' + ('mom' | translate) | fooBar">` ,
41
- match : [ `]="'Hello, ' + ('mom' | translate) | fooBar"` ]
45
+ match : [ `]="'Hello, ' + ('mom' | translate) | fooBar"` ] ,
42
46
} ,
43
47
{
44
48
testCase : `<edge-wizard-step [label]="'Restore Options' | translate" [validatingMessage]="'Processing archive...'|translate"` ,
45
- match : [ `]="'Restore Options' | translate"` , `]="'Processing archive...'|translate"` ]
46
- }
47
- ] ) ( 'Case: $testCase; Match: $match' , ( { testCase, match} ) => {
49
+ match : [
50
+ `]="'Restore Options' | translate"` ,
51
+ `]="'Processing archive...'|translate"` ,
52
+ ] ,
53
+ } ,
54
+ ] ) ( 'Case: $testCase; Match: $match' , ( { testCase, match } ) => {
48
55
const regex = new RegExp ( PIPE_IN_BINDING_REGEX , 'gm' ) ;
49
56
const result = testCase . match ( regex ) ;
50
57
@@ -56,33 +63,35 @@ describe('ngx-translate migration', () => {
56
63
test . each ( [
57
64
{
58
65
testCase : `<component>{{ "hello.world" | translate }}</component>` ,
59
- match : [ `{{ "hello.world" | translate }}` ]
66
+ match : [ `{{ "hello.world" | translate }}` ] ,
60
67
} ,
61
68
{
62
69
testCase : `<component>{{ "hello.world" | translate | anotherPipe | oneMore }}</component>` ,
63
- match : [ `{{ "hello.world" | translate | anotherPipe | oneMore }}` ]
70
+ match : [ `{{ "hello.world" | translate | anotherPipe | oneMore }}` ] ,
64
71
} ,
65
72
{
66
73
testCase : `<component>{{ "hello" | translate: { name: 'John' } }}</component>` ,
67
- match : [ `{{ "hello" | translate: { name: 'John' } }}` ]
74
+ match : [ `{{ "hello" | translate: { name: 'John' } }}` ] ,
68
75
} ,
69
76
{
70
77
testCase : `<component>{{ titleMap[reportType] | translate }}</component>` ,
71
- match : [ `{{ titleMap[reportType] | translate }}` ]
78
+ match : [ `{{ titleMap[reportType] | translate }}` ] ,
72
79
} ,
73
80
{
74
81
testCase : `<component>{{ ('foo.bar' | translate) + ': ' + (value | number: '1.0-2') }}</component>` ,
75
- match : [ `{{ ('foo.bar' | translate) + ': ' + (value | number: '1.0-2') }}` ]
82
+ match : [
83
+ `{{ ('foo.bar' | translate) + ': ' + (value | number: '1.0-2') }}` ,
84
+ ] ,
76
85
} ,
77
86
{
78
87
testCase : `<compnent>{{ 'Hello, ' + ('mom' | translate) | fooBar }}</compnent>` ,
79
- match : [ `{{ 'Hello, ' + ('mom' | translate) | fooBar }}` ]
88
+ match : [ `{{ 'Hello, ' + ('mom' | translate) | fooBar }}` ] ,
80
89
} ,
81
90
{
82
91
testCase : `{{"1" | translate}} {{errorCounter}} {{"2" | translate}}` ,
83
- match : [ `{{"1" | translate}}` , `{{"2" | translate}}` ]
84
- }
85
- ] ) ( 'Case: $testCase; Match: $match' , ( { testCase, match} ) => {
92
+ match : [ `{{"1" | translate}}` , `{{"2" | translate}}` ] ,
93
+ } ,
94
+ ] ) ( 'Case: $testCase; Match: $match' , ( { testCase, match } ) => {
86
95
const regex = new RegExp ( PIPE_REGEX , 'gm' ) ;
87
96
const result = testCase . match ( regex ) ;
88
97
@@ -92,106 +101,117 @@ describe('ngx-translate migration', () => {
92
101
} ) ;
93
102
94
103
describe ( 'Negative regex tests' , ( ) => {
95
-
96
104
describe ( 'Pipe in binding' , ( ) => {
97
105
test . each ( [
98
106
{
99
- testCase : `<component [header]="'hello.world' | transloco">`
107
+ testCase : `<component [header]="'hello.world' | transloco">` ,
100
108
} ,
101
109
{
102
- testCase : `<component [header]="'hello.world' | somePipe | anotherPipe">`
110
+ testCase : `<component [header]="'hello.world' | somePipe | anotherPipe">` ,
103
111
} ,
104
112
{
105
- testCase : `<component [header]="'hello' | transloco:params | anotherPipe">`
113
+ testCase : `<component [header]="'hello' | transloco:params | anotherPipe">` ,
106
114
} ,
107
115
{
108
- testCase : `<component [title]="titleMap[reportType] | fooBar">`
116
+ testCase : `<component [title]="titleMap[reportType] | fooBar">` ,
109
117
} ,
110
118
{
111
- testCase : `<component [matTooltip]="('foo.bar' | transloco) + ': ' + (value | number: '1.0-2')">`
119
+ testCase : `<component [matTooltip]="('foo.bar' | transloco) + ': ' + (value | number: '1.0-2')">` ,
112
120
} ,
113
121
{
114
- testCase : `<compnent [title]="'Hello World ' + ('mom' | transloco) | fooBar">`
122
+ testCase : `<compnent [title]="'Hello World ' + ('mom' | transloco) | fooBar">` ,
115
123
} ,
116
124
{
117
125
testCase : `<a [title]="'admin.1' | lowercase
118
126
| translate"
119
- </a>`
120
- }
121
- ] ) ( 'Case: $testCase' , ( { testCase} ) => {
127
+ </a>` ,
128
+ } ,
129
+ ] ) ( 'Case: $testCase' , ( { testCase } ) => {
122
130
const regex = new RegExp ( PIPE_IN_BINDING_REGEX , 'gm' ) ;
123
131
const result = testCase . match ( regex ) ;
124
132
125
133
expect ( result ) . toBeNull ( ) ;
126
134
} ) ;
127
135
} ) ;
128
-
136
+
129
137
describe ( 'Pipe' , ( ) => {
130
138
test . each ( [
131
139
{
132
- testCase : `<component>{{ "hello.world" | transloco }}</component>`
140
+ testCase : `<component>{{ "hello.world" | transloco }}</component>` ,
133
141
} ,
134
142
{
135
- testCase : `<component>{{ "hello.world" | transloco | anotherPipe | oneMore }}</component>`
143
+ testCase : `<component>{{ "hello.world" | transloco | anotherPipe | oneMore }}</component>` ,
136
144
} ,
137
145
{
138
- testCase : `<component>{{ "hello" | transloco: { name: 'John' } }}</component>`
146
+ testCase : `<component>{{ "hello" | transloco: { name: 'John' } }}</component>` ,
139
147
} ,
140
148
{
141
- testCase : `<component>{{ titleMap[reportType] | somePipe }}</component>`
149
+ testCase : `<component>{{ titleMap[reportType] | somePipe }}</component>` ,
142
150
} ,
143
151
{
144
- testCase : `<component>{{ ('foo.bar' | transloco) + ': ' + (value | number: '1.0-2') }}</component>`
152
+ testCase : `<component>{{ ('foo.bar' | transloco) + ': ' + (value | number: '1.0-2') }}</component>` ,
145
153
} ,
146
154
{
147
- testCase : `<compnent>{{ 'Hello, ' + ('mom' | transloco) | fooBar }}</compnent>`
148
- }
149
- ] ) ( 'Case: $testCase' , ( { testCase} ) => {
155
+ testCase : `<compnent>{{ 'Hello, ' + ('mom' | transloco) | fooBar }}</compnent>` ,
156
+ } ,
157
+ ] ) ( 'Case: $testCase' , ( { testCase } ) => {
150
158
const regex = new RegExp ( PIPE_REGEX , 'gm' ) ;
151
159
const result = testCase . match ( regex ) ;
152
160
153
161
expect ( result ) . toBeNull ( ) ;
154
162
} ) ;
155
163
} ) ;
156
-
157
164
} ) ;
158
165
159
166
describe ( 'HTML template' , ( ) => {
160
-
161
167
it ( 'should replace html template content' , async ( ) => {
162
168
const replacements : Record < string , string > = { } ,
163
169
isWindows = process . platform === 'win32' ;
164
-
170
+
165
171
( replaceInFile as unknown as jest . Mock ) . mockImplementation (
166
172
async ( config : ReplaceInFileConfig ) : Promise < void > => {
167
173
const path = config . files as string ,
168
174
regex = config . from as RegExp ,
169
175
replacer = config . to as ( match : string ) => string ;
170
-
171
- const files = await glob ( path , { windowsPathsNoEscape : isWindows } ) ;
172
-
176
+
177
+ const files = await glob ( path , { windowsPathsNoEscape : isWindows } ) ;
178
+
173
179
for ( const fullPath of files ) {
174
180
const filename = nodePath . parse ( fullPath ) . base ,
175
- content = replacements [ filename ] ?? await readFile ( fullPath , { encoding : 'utf-8' } ) ;
176
-
181
+ content =
182
+ replacements [ filename ] ??
183
+ ( await readFile ( fullPath , { encoding : 'utf-8' } ) ) ;
184
+
177
185
replacements [ filename ] = content . replace ( regex , replacer ) ;
178
186
}
179
- }
187
+ } ,
180
188
) ;
181
189
182
- const ngxTranslateTemplatePath = './src/tests/templates/pipes/ngx-translate' ;
190
+ const ngxTranslateTemplatePath =
191
+ './src/tests/templates/pipes/ngx-translate' ;
183
192
184
193
await run ( ngxTranslateTemplatePath ) ;
185
194
195
+ expect ( replaceInFile ) . toHaveBeenLastCalledWith (
196
+ expect . objectContaining ( {
197
+ glob : {
198
+ windowsPathsNoEscape : isWindows ,
199
+ } ,
200
+ } ) ,
201
+ ) ;
202
+
186
203
const filenames = Object . keys ( replacements ) ;
187
204
188
- for ( const filename of filenames ) {
189
- const resultPath = nodePath . join ( __dirname , './templates/pipes/transloco' , filename ) ,
190
- resultContent = await readFile ( resultPath , { encoding : 'utf-8' } ) ;
205
+ for ( const filename of filenames ) {
206
+ const resultPath = nodePath . join (
207
+ __dirname ,
208
+ './templates/pipes/transloco' ,
209
+ filename ,
210
+ ) ,
211
+ resultContent = await readFile ( resultPath , { encoding : 'utf-8' } ) ;
191
212
192
213
expect ( replacements [ filename ] ) . toBe ( resultContent ) ;
193
214
}
194
215
} ) ;
195
-
196
216
} ) ;
197
217
} ) ;
0 commit comments