forked from ucfopen/Obojobo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasset_resolver.test.js
More file actions
93 lines (72 loc) · 3.15 KB
/
Copy pathasset_resolver.test.js
File metadata and controls
93 lines (72 loc) · 3.15 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
// mock a fake webpack manifest
jest.mock(
'../server/public/compiled/manifest.json',
() => ({
'test.js': '/static-from-manifest/test.js',
'test.css': '/static-from-manifest/test.css',
'subdir/test.json': '/static-from-manifest/subdir/test.json'
}),
{ virtual: true }
)
const { assetForEnv } = require('../server/asset_resolver')
describe('Asset Resolver', () => {
const originalNODE_ENV = process.env.NODE_ENV
const originalIS_WEBPACK = process.env.IS_WEBPACK
afterAll(() => {
process.env.NODE_ENV = originalNODE_ENV
process.env.IS_WEBPACK = originalIS_WEBPACK
})
beforeEach(() => {
jest.resetModules()
delete process.env.NODE_ENV
delete process.env.ASSET_ENV
delete process.env.IS_WEBPACK
})
test('assetForEnv builds pattern for dev server', () => {
const path = assetForEnv('mock/path/item$[.full|.min].js')
expect(path).toEqual('mock/path/item.full.js')
})
test('assetForEnv builds pattern for dev server with nickname', () => {
const path = assetForEnv('mock/path/item$[.full|.min].js', 'dev')
expect(path).toEqual('mock/path/item.full.js')
})
test('assetForEnv builds pattern for test server', () => {
const path = assetForEnv('mock/path/item$[.full|.min].js', 'test')
expect(path).toEqual('mock/path/item.full.js')
})
test('assetForEnv builds pattern for production server', () => {
const path = assetForEnv('mock/path/item$[.full|.min].js', 'production')
expect(path).toEqual('mock/path/item.min.js')
})
test('assetForEnv builds pattern for production server with nickname', () => {
const path = assetForEnv('mock/path/item$[.full|.min].js', 'prod')
expect(path).toEqual('mock/path/item.min.js')
})
test('assertForEnv responds to process.env.ASSET_ENV', () => {
const beforeSet = assetForEnv('mock/path/item$[.full|.min].js')
expect(beforeSet).toEqual('mock/path/item.full.js')
process.env.ASSET_ENV = 'prod'
const afterSet = assetForEnv('mock/path/item$[.full|.min].js')
expect(afterSet).toEqual('mock/path/item.min.js')
const afterSetWithForce = assetForEnv('mock/path/item$[.full|.min].js', 'dev')
expect(afterSetWithForce).toEqual('mock/path/item.full.js')
})
test('webpackAssetPath returns expected paths when using webpack dev server', () => {
process.env.IS_WEBPACK = 'true'
const { webpackAssetPath } = require('../server/asset_resolver')
expect(webpackAssetPath('test.js')).toBe('/static/test.js')
expect(webpackAssetPath('test.css')).toBe('/static/test.css')
expect(webpackAssetPath('subdir/test.json')).toBe('/static/subdir/test.json')
expect(webpackAssetPath('asset-that-doesnt-exist.js')).toBe(
'/static/asset-that-doesnt-exist.js'
)
})
test('webpackAssetPath returns expected paths when NOT using webpack dev server', () => {
process.env.IS_WEBPACK = 'false'
const { webpackAssetPath } = require('../server/asset_resolver')
expect(webpackAssetPath('test.js')).toBe('/static-from-manifest/test.js')
expect(webpackAssetPath('test.css')).toBe('/static-from-manifest/test.css')
expect(webpackAssetPath('subdir/test.json')).toBe('/static-from-manifest/subdir/test.json')
expect(webpackAssetPath('asset-that-doesnt-exist.js')).toBe(undefined) //eslint-disable-line no-undefined
})
})