-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { getPublicEnv } from './get-public-env'; | ||
|
||
describe('getPublicEnv', () => { | ||
afterEach(() => { | ||
delete process.env.FOO; | ||
delete process.env.BAR; | ||
delete process.env.BAR; | ||
delete process.env.NEXT_PUBLIC_FOO; | ||
delete process.env.NEXT_PUBLIC_BAR; | ||
delete process.env.NEXT_PUBLIC_BAZ; | ||
}); | ||
|
||
it('should return a allow-listed value', () => { | ||
process.env.NEXT_PUBLIC_FOO = 'foo'; | ||
|
||
expect(getPublicEnv()).toEqual({ | ||
NEXT_PUBLIC_FOO: 'foo', | ||
}); | ||
}); | ||
|
||
it('should return multiple allow-listed values', () => { | ||
process.env.NEXT_PUBLIC_FOO = 'foo'; | ||
process.env.NEXT_PUBLIC_BAR = 'bar'; | ||
process.env.NEXT_PUBLIC_BAZ = 'baz'; | ||
|
||
expect(getPublicEnv()).toEqual({ | ||
NEXT_PUBLIC_FOO: 'foo', | ||
NEXT_PUBLIC_BAR: 'bar', | ||
NEXT_PUBLIC_BAZ: 'baz', | ||
}); | ||
}); | ||
|
||
it('should not return a non allow-listed value', () => { | ||
process.env.FOO = 'foo'; | ||
|
||
expect(getPublicEnv()).toEqual({}); | ||
}); | ||
|
||
it('should not return multiple non allow-listed values', () => { | ||
process.env.FOO = 'foo'; | ||
process.env.BAR = 'bar'; | ||
process.env.BAZ = 'baz'; | ||
|
||
expect(getPublicEnv()).toEqual({}); | ||
}); | ||
|
||
it('should not return a mixed list of allow-listed and non allow-listed values', () => { | ||
process.env.NEXT_PUBLIC_FOO = 'foo'; | ||
process.env.BAR = 'bar'; | ||
process.env.NEXT_PUBLIC_BAZ = 'baz'; | ||
|
||
expect(getPublicEnv()).toEqual({ | ||
NEXT_PUBLIC_FOO: 'foo', | ||
NEXT_PUBLIC_BAZ: 'baz', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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`; | ||
|
||
beforeAll(() => { | ||
infoSpy.mockImplementation(); | ||
|
||
fs.mkdirSync(path); | ||
}); | ||
|
||
afterAll(() => { | ||
infoSpy.mockRestore(); | ||
|
||
fs.rmdirSync(path); | ||
}); | ||
|
||
describe('writeBrowserEnv', () => { | ||
afterEach(() => { | ||
fs.rmSync(file); | ||
}); | ||
|
||
it('should write an empty env', () => { | ||
writeBrowserEnv({}); | ||
|
||
expect(infoSpy).toHaveBeenCalledWith( | ||
'next-runtime-env: Writing browser runtime env', | ||
file | ||
); | ||
|
||
const content = fs.readFileSync(file).toString(); | ||
|
||
expect(content).toEqual('window.__ENV = {};'); | ||
}); | ||
|
||
it('should write and env with a value', () => { | ||
writeBrowserEnv({ | ||
NEXT_PUBLIC_FOO: 'foo', | ||
}); | ||
|
||
expect(infoSpy).toHaveBeenCalledWith( | ||
'next-runtime-env: Writing browser runtime env', | ||
file | ||
); | ||
|
||
const content = fs.readFileSync(file).toString(); | ||
|
||
expect(content).toEqual('window.__ENV = {"NEXT_PUBLIC_FOO":"foo"};'); | ||
}); | ||
|
||
it('should write and env with multiple values', () => { | ||
writeBrowserEnv({ | ||
NEXT_PUBLIC_FOO: 'foo', | ||
NEXT_PUBLIC_BAR: 'bar', | ||
NEXT_PUBLIC_BAZ: 'baz', | ||
}); | ||
|
||
expect(infoSpy).toHaveBeenCalledWith( | ||
'next-runtime-env: Writing browser runtime env', | ||
file | ||
); | ||
|
||
const content = fs.readFileSync(file).toString(); | ||
|
||
expect(content).toEqual( | ||
'window.__ENV = {"NEXT_PUBLIC_FOO":"foo","NEXT_PUBLIC_BAR":"bar","NEXT_PUBLIC_BAZ":"baz"};' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters