@@ -6,7 +6,7 @@ const express = require("express");
6
6
const sinon = require ( "sinon" ) ;
7
7
const translations = require ( "../../translations/translations" ) ;
8
8
9
- describe ( "Translations " , ( ) => {
9
+ describe ( "translations " , ( ) => {
10
10
let server ;
11
11
12
12
beforeAll ( ( ) => {
@@ -26,8 +26,9 @@ describe("Translations", () => {
26
26
} ) ;
27
27
28
28
it ( "should have a translation file in the specified path" , ( ) => {
29
- for ( let language in translations ) {
29
+ for ( const language in translations ) {
30
30
const file = fs . statSync ( translations [ language ] ) ;
31
+
31
32
expect ( file . isFile ( ) ) . toBe ( true ) ;
32
33
}
33
34
} ) ;
@@ -36,90 +37,91 @@ describe("Translations", () => {
36
37
let dom ;
37
38
38
39
beforeEach ( ( ) => {
39
- dom = new JSDOM (
40
- `<script>var Translator = {}; var Log = {log: () => {}}; var config = {language: 'de'};</script>\
41
- <script src="file://${ path . join ( __dirname , ".." , ".." , "js" , "class.js" ) } "></script>\
42
- <script src="file://${ path . join ( __dirname , ".." , ".." , "js" , "module.js" ) } "></script>` ,
43
- { runScripts : "dangerously" , resources : "usable" }
44
- ) ;
40
+ // Create a new JSDOM instance for each test
41
+ dom = new JSDOM ( "" , { runScripts : "dangerously" , resources : "usable" } ) ;
42
+
43
+ // Mock the necessary global objects
44
+ dom . window . Log = { log : jest . fn ( ) , error : jest . fn ( ) } ;
45
+ dom . window . Translator = { } ;
46
+ dom . window . config = { language : "de" } ;
47
+
48
+ // Load class.js and module.js content directly
49
+ const classJs = fs . readFileSync ( path . join ( __dirname , ".." , ".." , "js" , "class.js" ) , "utf-8" ) ;
50
+ const moduleJs = fs . readFileSync ( path . join ( __dirname , ".." , ".." , "js" , "module.js" ) , "utf-8" ) ;
51
+
52
+ // Execute the scripts in the JSDOM context
53
+ dom . window . eval ( classJs ) ;
54
+ dom . window . eval ( moduleJs ) ;
45
55
} ) ;
46
56
47
- it ( "should load translation file" , ( ) => {
48
- return new Promise ( ( done ) => {
49
- dom . window . onload = async ( ) => {
50
- const { Translator, Module, config } = dom . window ;
51
- config . language = "en" ;
52
- Translator . load = sinon . stub ( ) . callsFake ( ( _m , _f , _fb ) => null ) ;
57
+ it ( "should load translation file" , async ( ) => {
58
+ await new Promise ( ( resolve ) => {
59
+ dom . window . onload = resolve ;
60
+ } ) ;
53
61
54
- Module . register ( "name" , { getTranslations : ( ) => translations } ) ;
55
- const MMM = Module . create ( "name" ) ;
62
+ const { Translator, Module, config } = dom . window ;
63
+ config . language = "en" ;
64
+ Translator . load = sinon . stub ( ) . callsFake ( ( _m , _f , _fb ) => null ) ;
56
65
57
- await MMM . loadTranslations ( ) ;
66
+ Module . register ( "name" , { getTranslations : ( ) => translations } ) ;
67
+ const MMM = Module . create ( "name" ) ;
58
68
59
- expect ( Translator . load . args ) . toHaveLength ( 1 ) ;
60
- expect ( Translator . load . calledWith ( MMM , "translations/en.json" , false ) ) . toBe ( true ) ;
69
+ await MMM . loadTranslations ( ) ;
61
70
62
- done ( ) ;
63
- } ;
64
- } ) ;
71
+ expect ( Translator . load . args ) . toHaveLength ( 1 ) ;
72
+ expect ( Translator . load . calledWith ( MMM , "translations/en.json" , false ) ) . toBe ( true ) ;
65
73
} ) ;
66
74
67
- it ( "should load translation + fallback file" , ( ) => {
68
- return new Promise ( ( done ) => {
69
- dom . window . onload = async ( ) => {
70
- const { Translator, Module } = dom . window ;
71
- Translator . load = sinon . stub ( ) . callsFake ( ( _m , _f , _fb ) => null ) ;
75
+ it ( "should load translation + fallback file" , async ( ) => {
76
+ await new Promise ( ( resolve ) => {
77
+ dom . window . onload = resolve ;
78
+ } ) ;
72
79
73
- Module . register ( "name" , { getTranslations : ( ) => translations } ) ;
74
- const MMM = Module . create ( "name" ) ;
80
+ const { Translator , Module } = dom . window ;
81
+ Translator . load = sinon . stub ( ) . callsFake ( ( _m , _f , _fb ) => null ) ;
75
82
76
- await MMM . loadTranslations ( ) ;
83
+ Module . register ( "name" , { getTranslations : ( ) => translations } ) ;
84
+ const MMM = Module . create ( "name" ) ;
77
85
78
- expect ( Translator . load . args ) . toHaveLength ( 2 ) ;
79
- expect ( Translator . load . calledWith ( MMM , "translations/de.json" , false ) ) . toBe ( true ) ;
80
- expect ( Translator . load . calledWith ( MMM , "translations/en.json" , true ) ) . toBe ( true ) ;
86
+ await MMM . loadTranslations ( ) ;
81
87
82
- done ( ) ;
83
- } ;
84
- } ) ;
88
+ expect ( Translator . load . args ) . toHaveLength ( 2 ) ;
89
+ expect ( Translator . load . calledWith ( MMM , "translations/de.json" , false ) ) . toBe ( true ) ;
90
+ expect ( Translator . load . calledWith ( MMM , "translations/en.json" , true ) ) . toBe ( true ) ;
85
91
} ) ;
86
92
87
- it ( "should load translation fallback file" , ( ) => {
88
- return new Promise ( ( done ) => {
89
- dom . window . onload = async ( ) => {
90
- const { Translator, Module, config } = dom . window ;
91
- config . language = "--" ;
92
- Translator . load = sinon . stub ( ) . callsFake ( ( _m , _f , _fb ) => null ) ;
93
+ it ( "should load translation fallback file" , async ( ) => {
94
+ await new Promise ( ( resolve ) => {
95
+ dom . window . onload = resolve ;
96
+ } ) ;
93
97
94
- Module . register ( "name" , { getTranslations : ( ) => translations } ) ;
95
- const MMM = Module . create ( "name" ) ;
98
+ const { Translator, Module, config } = dom . window ;
99
+ config . language = "--" ;
100
+ Translator . load = sinon . stub ( ) . callsFake ( ( _m , _f , _fb ) => null ) ;
96
101
97
- await MMM . loadTranslations ( ) ;
102
+ Module . register ( "name" , { getTranslations : ( ) => translations } ) ;
103
+ const MMM = Module . create ( "name" ) ;
98
104
99
- expect ( Translator . load . args ) . toHaveLength ( 1 ) ;
100
- expect ( Translator . load . calledWith ( MMM , "translations/en.json" , true ) ) . toBe ( true ) ;
105
+ await MMM . loadTranslations ( ) ;
101
106
102
- done ( ) ;
103
- } ;
104
- } ) ;
107
+ expect ( Translator . load . args ) . toHaveLength ( 1 ) ;
108
+ expect ( Translator . load . calledWith ( MMM , "translations/en.json" , true ) ) . toBe ( true ) ;
105
109
} ) ;
106
110
107
- it ( "should load no file" , ( ) => {
108
- return new Promise ( ( done ) => {
109
- dom . window . onload = async ( ) => {
110
- const { Translator, Module } = dom . window ;
111
- Translator . load = sinon . stub ( ) ;
111
+ it ( "should load no file" , async ( ) => {
112
+ await new Promise ( ( resolve ) => {
113
+ dom . window . onload = resolve ;
114
+ } ) ;
112
115
113
- Module . register ( "name" , { } ) ;
114
- const MMM = Module . create ( "name" ) ;
116
+ const { Translator , Module } = dom . window ;
117
+ Translator . load = sinon . stub ( ) ;
115
118
116
- await MMM . loadTranslations ( ) ;
119
+ Module . register ( "name" , { } ) ;
120
+ const MMM = Module . create ( "name" ) ;
117
121
118
- expect ( Translator . load . callCount ) . toBe ( 0 ) ;
122
+ await MMM . loadTranslations ( ) ;
119
123
120
- done ( ) ;
121
- } ;
122
- } ) ;
124
+ expect ( Translator . load . callCount ) . toBe ( 0 ) ;
123
125
} ) ;
124
126
} ) ;
125
127
@@ -130,29 +132,30 @@ describe("Translations", () => {
130
132
}
131
133
} ;
132
134
133
- describe ( "Parsing language files through the Translator class" , ( ) => {
134
- for ( let language in translations ) {
135
- it ( `should parse ${ language } ` , ( ) => {
136
- return new Promise ( ( done ) => {
137
- const dom = new JSDOM (
138
- `<script>var translations = ${ JSON . stringify ( translations ) } ; var Log = {log: () => {}};</script>\
139
- <script src="file://${ path . join ( __dirname , ".." , ".." , "js" , "translator.js" ) } ">` ,
140
- { runScripts : "dangerously" , resources : "usable" }
141
- ) ;
142
- dom . window . onload = async ( ) => {
143
- const { Translator } = dom . window ;
144
-
145
- await Translator . load ( mmm , translations [ language ] , false ) ;
146
- expect ( typeof Translator . translations [ mmm . name ] ) . toBe ( "object" ) ;
147
- expect ( Object . keys ( Translator . translations [ mmm . name ] ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
148
- done ( ) ;
149
- } ;
135
+ const translatorJs = fs . readFileSync ( path . join ( __dirname , ".." , ".." , "js" , "translator.js" ) , "utf-8" ) ;
136
+
137
+ describe ( "parsing language files through the Translator class" , ( ) => {
138
+ for ( const language in translations ) {
139
+ it ( `should parse ${ language } ` , async ( ) => {
140
+ const dom = new JSDOM ( "" , { runScripts : "dangerously" , resources : "usable" } ) ;
141
+ dom . window . Log = { log : jest . fn ( ) } ;
142
+ dom . window . translations = translations ;
143
+ dom . window . eval ( translatorJs ) ;
144
+
145
+ await new Promise ( ( resolve ) => {
146
+ dom . window . onload = resolve ;
150
147
} ) ;
148
+
149
+ const { Translator } = dom . window ;
150
+ await Translator . load ( mmm , translations [ language ] , false ) ;
151
+
152
+ expect ( typeof Translator . translations [ mmm . name ] ) . toBe ( "object" ) ;
153
+ expect ( Object . keys ( Translator . translations [ mmm . name ] ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
151
154
} ) ;
152
155
}
153
156
} ) ;
154
157
155
- describe ( "Same keys" , ( ) => {
158
+ describe ( "same keys" , ( ) => {
156
159
let base ;
157
160
158
161
// Some expressions are not easy to translate automatically. For the sake of a working test, we filter them out.
@@ -178,7 +181,6 @@ describe("Translations", () => {
178
181
const dom = new JSDOM ( "" , { runScripts : "dangerously" , resources : "usable" } ) ;
179
182
dom . window . Log = { log : jest . fn ( ) } ;
180
183
dom . window . translations = translations ;
181
- const translatorJs = fs . readFileSync ( path . join ( __dirname , ".." , ".." , "js" , "translator.js" ) , "utf-8" ) ;
182
184
dom . window . eval ( translatorJs ) ;
183
185
184
186
return new Promise ( ( resolve ) => {
0 commit comments