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