This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.js
93 lines (67 loc) · 3.58 KB
/
index.spec.js
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
import { expect } from 'chai';
import path from 'path';
import transformCode from './transformCode';
function getFixtures(name) {
return path.resolve(__dirname, 'fixtures', name);
}
describe('index', () => {
const baseConfig = {
baseUri: 'http://cdn.address',
baseDir: '/assets',
};
it('should replace import statements with uri', () => {
const result = transformCode(getFixtures('import-image.js'), baseConfig).code;
expect(result).to.equal('const test = "http://cdn.address/assets/path/to/icon.svg";');
});
it('should let you flatten the file path', () => {
const config = { ...baseConfig, flatten: true };
const result = transformCode(getFixtures('import-image.js'), config).code;
expect(result).to.equal('const test = "http://cdn.address/assets/icon.svg";');
});
it('should replace import statements with uri and hash suffix of content', () => {
const config = { ...baseConfig, hash: 'suffix', baseDir: '/' };
const result = transformCode(getFixtures('import-uri-hash.js'), config).code;
expect(result).to.equal('const test = "http://cdn.address/icon.svg?80c59330";');
});
it('should replace import statements with uri and hash of content s filename', () => {
const config = { ...baseConfig, hash: true, baseDir: '/' };
const result = transformCode(getFixtures('import-uri-hash.js'), config).code;
expect(result).to.equal('const test = "http://cdn.address/80c59330e4b57f48e2f365dcb95140d9350bb816.svg";');
});
it('should replace import statements with uri when base uri and dir not defined', () => {
const result = transformCode(getFixtures('import-image.js')).code;
expect(result).to.equal('const test = "icon.svg";');
});
it('should replace import statements with uri when base dir not defined', () => {
const result = transformCode(getFixtures('import-image.js'), {
baseUri: baseConfig.baseUri,
}).code;
expect(result).to.equal('const test = "http://cdn.address/icon.svg";');
});
it('should replace require statements with uri', () => {
const result = transformCode(getFixtures('require-image.js'), baseConfig).code;
expect(result).to.equal('const test = "http://cdn.address/assets/path/to/icon.svg";');
});
it('should do nothing when imports have no extensions', () => {
const result = transformCode(getFixtures('import-no-ext.js')).code;
expect(result).to.equal("import test from 'something';");
});
it('should do nothing when require have no extensions', () => {
const result = transformCode(getFixtures('require-no-ext.js')).code;
expect(result).to.equal("const test = require('something');");
});
it('should do nothing when not a require assignment', () => {
const result = transformCode(getFixtures('require-var.js')).code;
expect(result).to.equal("const test = 'something';");
});
it('should hash filename', () => {
const config = { ...baseConfig, hash: 'filename', baseUri: '', baseDir: '' };
const result = transformCode(getFixtures('import-icon.js'), config).code;
expect(result).to.equal('const test = "80c59330e4b57f48e2f365dcb95140d9350bb816.svg";');
});
it('should replace baseUri from env', () => {
const config = { ...baseConfig, envName: 'ASSET_BASE' };
const result = transformCode(getFixtures('import-image.js'), config).code;
expect(result).to.equal('const test = (process.env.ASSET_BASE || "") + "/assets/path/to/icon.svg";');
});
});