-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathwrite-browser-env.spec.ts
More file actions
105 lines (71 loc) · 2.73 KB
/
Copy pathwrite-browser-env.spec.ts
File metadata and controls
105 lines (71 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import chalk from 'chalk';
import fs from 'fs';
import { writeBrowserEnv } from './write-browser-env';
const infoSpy = jest.spyOn(console, 'info');
const base = fs.realpathSync(process.cwd());
const path = `${base}/public`;
const file = `${path}/__ENV.js`;
const message = `- ${chalk.green(
`ready`,
)} [next-runtime-env] wrote browser runtime environment variables to '${file}'.`;
beforeAll(() => {
infoSpy.mockImplementation();
fs.mkdirSync(path);
});
afterAll(() => {
infoSpy.mockRestore();
fs.rmSync(path, { recursive: true, force: true });
});
describe('writeBrowserEnv()', () => {
it('should write an empty env', () => {
writeBrowserEnv({});
expect(infoSpy).toHaveBeenCalledWith(message);
const content = fs.readFileSync(file).toString();
expect(content).toEqual('window.__ENV = {};');
fs.rmSync(file);
});
it('should write an env with a value', () => {
writeBrowserEnv({
NEXT_PUBLIC_FOO: 'foo',
});
expect(infoSpy).toHaveBeenCalledWith(message);
const content = fs.readFileSync(file).toString();
expect(content).toEqual('window.__ENV = {"NEXT_PUBLIC_FOO":"foo"};');
fs.rmSync(file);
});
it('should write an env with multiple values', () => {
writeBrowserEnv({
NEXT_PUBLIC_FOO: 'foo',
NEXT_PUBLIC_BAR: 'bar',
NEXT_PUBLIC_BAZ: 'baz',
});
expect(infoSpy).toHaveBeenCalledWith(message);
const content = fs.readFileSync(file).toString();
expect(content).toEqual(
'window.__ENV = {"NEXT_PUBLIC_FOO":"foo","NEXT_PUBLIC_BAR":"bar","NEXT_PUBLIC_BAZ":"baz"};',
);
fs.rmSync(file);
});
it('should write to a rootdirectory', () => {
const fileInRootdirectory = `${base}/rootdirectory/public/__ENV.js`;
const messageWithRootdirectory = `- ${chalk.green(
`ready`,
)} [next-runtime-env] wrote browser runtime environment variables to '${fileInRootdirectory}'.`;
writeBrowserEnv({}, { rootdirectory: 'rootdirectory/' });
expect(infoSpy).toHaveBeenCalledWith(messageWithRootdirectory);
const content = fs.readFileSync(fileInRootdirectory).toString();
expect(content).toEqual('window.__ENV = {};');
fs.rmSync(fileInRootdirectory);
});
it('should write to a subdirectory', () => {
const fileInSubdirectory = `${path}/subdirectory/__ENV.js`;
const messageWithSubdirectory = `- ${chalk.green(
`ready`,
)} [next-runtime-env] wrote browser runtime environment variables to '${fileInSubdirectory}'.`;
writeBrowserEnv({}, { subdirectory: 'subdirectory/' });
expect(infoSpy).toHaveBeenCalledWith(messageWithSubdirectory);
const content = fs.readFileSync(fileInSubdirectory).toString();
expect(content).toEqual('window.__ENV = {};');
fs.rmSync(fileInSubdirectory);
});
});