Skip to content

Commit 281e944

Browse files
committed
test: generateModuleText coverage
1 parent d064dbd commit 281e944

File tree

2 files changed

+191
-1
lines changed

2 files changed

+191
-1
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`generateModuleText creates config module 1`] = `
4+
"
5+
const config = {\\"foo\\":\\"bar\\"};
6+
7+
export { config };
8+
export default config;
9+
10+
export function currentEnvironment() {
11+
return \\"test\\";
12+
}
13+
"
14+
`;
15+
16+
exports[`generateModuleText creates config module with esm validation function 1`] = `
17+
"
18+
const configValue = {\\"foo\\":\\"bar\\"};
19+
20+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
21+
22+
// if the global was already defined, use it
23+
const config = (globalNamespace._appConfig || configValue);
24+
25+
// if the global is frozen then it was set by electron and we can't change it, but we'll set it if we can
26+
if (
27+
typeof globalNamespace._appConfig === 'undefined' ||
28+
!Object.isFrozen(globalNamespace._appConfig)
29+
) {
30+
globalNamespace._appConfig = config;
31+
}
32+
33+
export { config };
34+
export default config;
35+
36+
const foo = 'bar';
37+
38+
39+
function genValidateConfig(){
40+
const validateConfigModule = {};
41+
(function(module){import foo from \\"bar\\";})(validateConfigModule);
42+
return validateConfigModule.exports;
43+
}
44+
45+
46+
export const validateConfig = /*#__PURE__*/ genValidateConfig();
47+
48+
export function currentEnvironment() {
49+
return \\"test\\";
50+
}
51+
"
52+
`;
53+
54+
exports[`generateModuleText creates config module with global namespace 1`] = `
55+
"
56+
const configValue = {\\"foo\\":\\"bar\\"};
57+
58+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
59+
60+
// if the global was already defined, use it
61+
const config = (globalNamespace._appConfig || configValue);
62+
63+
// if the global is frozen then it was set by electron and we can't change it, but we'll set it if we can
64+
if (
65+
typeof globalNamespace._appConfig === 'undefined' ||
66+
!Object.isFrozen(globalNamespace._appConfig)
67+
) {
68+
globalNamespace._appConfig = config;
69+
}
70+
71+
export { config };
72+
export default config;
73+
74+
export function currentEnvironment() {
75+
return \\"test\\";
76+
}
77+
"
78+
`;
79+
80+
exports[`generateModuleText creates config module with validation function 1`] = `
81+
"
82+
const configValue = {\\"foo\\":\\"bar\\"};
83+
84+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
85+
86+
// if the global was already defined, use it
87+
const config = (globalNamespace._appConfig || configValue);
88+
89+
// if the global is frozen then it was set by electron and we can't change it, but we'll set it if we can
90+
if (
91+
typeof globalNamespace._appConfig === 'undefined' ||
92+
!Object.isFrozen(globalNamespace._appConfig)
93+
) {
94+
globalNamespace._appConfig = config;
95+
}
96+
97+
export { config };
98+
export default config;
99+
100+
101+
function genValidateConfig(){
102+
const validateConfigModule = {};
103+
(function(module){
104+
const foo = 'bar';
105+
})(validateConfigModule);
106+
return validateConfigModule.exports;
107+
}
108+
109+
110+
export const validateConfig = /*#__PURE__*/ genValidateConfig();
111+
112+
export function currentEnvironment() {
113+
return \\"test\\";
114+
}
115+
"
116+
`;

app-config-utils/src/index.test.ts

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isNode, isObject, isPrimitive } from './index';
1+
import { isNode, isObject, isPrimitive, generateModuleText } from './index';
22

33
describe('isNode', () => {
44
it('detects node.js env', () => {
@@ -25,3 +25,77 @@ describe('isPrimitive', () => {
2525
expect(isPrimitive({})).toBe(false);
2626
});
2727
});
28+
29+
describe('generateModuleText', () => {
30+
it('creates config module', () => {
31+
expect(
32+
generateModuleText(
33+
{
34+
foo: 'bar',
35+
},
36+
{
37+
environment: 'test',
38+
useGlobalNamespace: false,
39+
esmValidationCode: false,
40+
validationFunctionCode: undefined,
41+
},
42+
),
43+
).toMatchSnapshot();
44+
});
45+
46+
it('creates config module with global namespace', () => {
47+
expect(
48+
generateModuleText(
49+
{
50+
foo: 'bar',
51+
},
52+
{
53+
environment: 'test',
54+
useGlobalNamespace: true,
55+
esmValidationCode: false,
56+
validationFunctionCode: undefined,
57+
},
58+
),
59+
).toMatchSnapshot();
60+
});
61+
62+
it('creates config module with validation function', () => {
63+
expect(
64+
generateModuleText(
65+
{
66+
foo: 'bar',
67+
},
68+
{
69+
environment: 'test',
70+
useGlobalNamespace: true,
71+
esmValidationCode: false,
72+
// @ts-ignore
73+
validationFunctionCode: () => {
74+
return `
75+
const foo = 'bar';
76+
`;
77+
},
78+
},
79+
),
80+
).toMatchSnapshot();
81+
});
82+
83+
it('creates config module with esm validation function', () => {
84+
expect(
85+
generateModuleText(
86+
{
87+
foo: 'bar',
88+
},
89+
{
90+
environment: 'test',
91+
useGlobalNamespace: true,
92+
esmValidationCode: true,
93+
// @ts-ignore
94+
validationFunctionCode: () => {
95+
return ['import foo from "bar";', `const foo = 'bar';`];
96+
},
97+
},
98+
),
99+
).toMatchSnapshot();
100+
});
101+
});

0 commit comments

Comments
 (0)