forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
158 lines (128 loc) · 4.28 KB
/
index.test.js
File metadata and controls
158 lines (128 loc) · 4.28 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import Preset from './index';
import Transpiler from '../transpilers/index';
function createDummyTranspiler(name: string) {
const Klass = class Trans extends Transpiler {
constructor() {
super(name);
}
};
return new Klass();
}
describe('sandbox', () => {
describe('preset', () => {
describe('query', () => {
const preset = new Preset('test', [], []);
preset.registerTranspiler(t => t.path.endsWith('.js'), [
{ transpiler: createDummyTranspiler('babel-loader') },
]);
preset.registerTranspiler(t => t.path.endsWith('.css'), [
{ transpiler: createDummyTranspiler('style-loader') },
{ transpiler: createDummyTranspiler('modules-loader') },
]);
it('generates the right query for 1 transpiler', () => {
const module = {
path: 'test.js',
code: '',
};
expect(preset.getQuery(module)).toEqual('!babel-loader');
});
it('generates the right query for 2 transpiler', () => {
const module = {
path: 'test.css',
code: '',
};
expect(preset.getQuery(module)).toEqual('!style-loader!modules-loader');
});
it('generates the right query for absolute custom query', () => {
const module = {
path: 'test.css',
code: '',
};
expect(preset.getQuery(module, '!babel-loader')).toEqual(
'!babel-loader'
);
});
it('generates the right query for custom query', () => {
const module = {
path: 'test.css',
code: '',
};
expect(preset.getQuery(module, 'babel-loader')).toEqual(
'!style-loader!modules-loader!babel-loader'
);
});
});
describe('alias', () => {
function createPreset(aliases) {
return new Preset('test', [], aliases);
}
it('finds the right simple alias', () => {
const preset = createPreset({
test: 'test2',
});
expect(preset.getAliasedPath('test')).toBe('test2');
});
it('chooses the right simple alias', () => {
const preset = createPreset({
test: 'test2',
testtest: 'test4',
});
expect(preset.getAliasedPath('testtest')).toBe('test4');
});
it('works with paths', () => {
const preset = createPreset({
test: 'test2',
testtest: 'test4',
});
expect(preset.getAliasedPath('test/piano/guitar')).toBe(
'test2/piano/guitar'
);
});
it('works with deeper paths', () => {
const preset = createPreset({
test: 'test4',
'test/piano': 'test2',
});
expect(preset.getAliasedPath('test/piano/guitar')).toBe('test2/guitar');
});
it('works in a real life scenario', () => {
const preset = createPreset({
preact$: 'preact',
// preact-compat aliases for supporting React dependencies:
react: 'preact-compat',
'react-dom': 'preact-compat',
'create-react-class': 'preact-compat/lib/create-react-class',
'react-addons-css-transition-group': 'preact-css-transition-group',
});
expect(preset.getAliasedPath('react/render')).toBe(
'preact-compat/render'
);
});
it("doesn't replace partial paths", () => {
const preset = createPreset({
preact$: 'preact',
// preact-compat aliases for supporting React dependencies:
react: 'preact-compat',
'react-dom': 'preact-compat',
'create-react-class': 'preact-compat/lib/create-react-class',
'react-addons-css-transition-group': 'preact-css-transition-group',
});
expect(preset.getAliasedPath('react-foo')).toBe('react-foo');
});
describe('exact alias', () => {
it('resolves an exact alias', () => {
const preset = createPreset({
vue$: 'vue/common/dist',
});
expect(preset.getAliasedPath('vue')).toBe('vue/common/dist');
});
it("doesnt't resolve a not exact alias", () => {
const preset = createPreset({
vue$: 'vue/common/dist',
});
expect(preset.getAliasedPath('vue/test')).toBe('vue/test');
});
});
});
});
});