forked from stenciljs/sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.spec.ts
175 lines (144 loc) · 6.21 KB
/
utils.spec.ts
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import * as d from '../src/declarations';
import * as util from '../src/util';
describe('getRenderOptions', () => {
const sourceText = 'body { color: blue; }';
const fileName = '/some/path/file-name.scss';
const context: d.PluginCtx = {
config: {
configPath: '/Users/my/app/stencil.config.ts',
rootDir: '/Users/my/app/',
srcDir: '/Users/my/app/src/',
},
fs: {} as any,
diagnostics: []
} as any;
it('should remove "file" config', () => {
const input: d.PluginOptions = {
file: '/my/global/variables.scss'
};
const output = util.getRenderOptions(input, sourceText, fileName, context);
expect(output.data).toBe(`body { color: blue; }`);
expect(output.file).toBeUndefined();
});
it('should inject global sass array, not change input options or include globals in output opts', () => {
const input: d.PluginOptions = {
injectGlobalPaths: ['/my/global/variables.scss']
};
const output = util.getRenderOptions(input, sourceText, fileName, context);
expect(output.data).toBe(`@use "/my/global/variables.scss";body { color: blue; }`);
expect(output.injectGlobalPaths).toBeUndefined();
expect(input.injectGlobalPaths).toHaveLength(1);
expect(input.injectGlobalPaths[0]).toBe('/my/global/variables.scss');
});
it('should inject global sass array, not change input options or include globals in output opts, and add namespace', () => {
const input: d.PluginOptions = {
injectGlobalPaths: [['/my/global/variables.scss', 'var']]
};
const output = util.getRenderOptions(input, sourceText, fileName, context);
expect(output.data).toBe(`@use "/my/global/variables.scss" as var;body { color: blue; }`);
expect(output.injectGlobalPaths).toBeUndefined();
expect(input.injectGlobalPaths).toHaveLength(1);
expect(input.injectGlobalPaths[0][0]).toBe('/my/global/variables.scss');
expect(input.injectGlobalPaths[0][1]).toBe('var');
});
it('should inject global sass array, not change input options or include globals in output opts, and discern when to add namespace', () => {
const input: d.PluginOptions = {
injectGlobalPaths: [
['/my/global/variables.scss', 'var'],
'/my/global/mixins.scss'
]
};
const output = util.getRenderOptions(input, sourceText, fileName, context);
expect(output.data).toBe(`@use "/my/global/variables.scss" as var;@use "/my/global/mixins.scss";body { color: blue; }`);
expect(output.injectGlobalPaths).toBeUndefined();
expect(input.injectGlobalPaths).toHaveLength(2);
expect(input.injectGlobalPaths[0][0]).toBe('/my/global/variables.scss');
expect(input.injectGlobalPaths[0][1]).toBe('var');
expect(input.injectGlobalPaths[1]).toBe('/my/global/mixins.scss');
});
it('should add dirname of filename to existing includePaths array and not change input options', () => {
const input: d.PluginOptions = {
includePaths: ['/some/other/include/path']
};
const output = util.getRenderOptions(input, sourceText, fileName, context);
expect(output.includePaths).toHaveLength(2);
expect(output.includePaths[0]).toBe('/some/other/include/path');
expect(output.includePaths[1]).toBe('/some/path');
expect(input.includePaths).toHaveLength(1);
expect(input.includePaths[0]).toBe('/some/other/include/path');
});
it('should add dirname of filename to includePaths and not change input options', () => {
const input: d.PluginOptions = {};
const output = util.getRenderOptions(input, sourceText, fileName, context);
expect(output.includePaths).toHaveLength(1);
expect(output.includePaths[0]).toBe('/some/path');
expect(input.includePaths).toBeUndefined();
});
it('should set data', () => {
const input: d.PluginOptions = {};
const output = util.getRenderOptions(input, sourceText, fileName, context);
expect(output.data).toBe(sourceText);
});
});
describe('usePlugin', () => {
it('should use the plugin for .scss file', () => {
const fileName = 'my-file.scss';
expect(util.usePlugin(fileName)).toBe(true);
});
it('should use the plugin for .sass file', () => {
const fileName = 'my-file.sass';
expect(util.usePlugin(fileName)).toBe(true);
});
it('should not use the plugin for .pcss file', () => {
const fileName = 'my-file.pcss';
expect(util.usePlugin(fileName)).toBe(false);
});
it('should not use the plugin for .css file', () => {
const fileName = 'my-file.css';
expect(util.usePlugin(fileName)).toBe(false);
});
});
describe('createResultsId', () => {
it('should change scss the extension to be css', () => {
const input = '/my/path/my-file.scss';
const output = util.createResultsId(input);
expect(output).toBe('/my/path/my-file.css');
});
it('should change sass the extension to be css', () => {
const input = '/my/path.DIR/my-file.whatever.SOME.dots.SASS';
const output = util.createResultsId(input);
expect(output).toBe('/my/path.DIR/my-file.whatever.SOME.dots.css');
});
});
describe('getModuleId', () => {
it('getModuleId non-scoped ~ package', () => {
const m = util.getModuleId('~ionicons/dist/css/ionicons.css');
expect(m.moduleId).toBe('ionicons');
expect(m.filePath).toBe('dist/css/ionicons.css');
});
it('getModuleId non-scoped package', () => {
const m = util.getModuleId('ionicons/dist/css/ionicons.css');
expect(m.moduleId).toBe('ionicons');
expect(m.filePath).toBe('dist/css/ionicons.css');
});
it('getModuleId non-scoped package, no path', () => {
const m = util.getModuleId('ionicons');
expect(m.moduleId).toBe('ionicons');
expect(m.filePath).toBe('');
});
it('getModuleId scoped ~ package', () => {
const m = util.getModuleId('~@ionic/core/dist/ionic/css/ionic.css');
expect(m.moduleId).toBe('@ionic/core');
expect(m.filePath).toBe('dist/ionic/css/ionic.css');
});
it('getModuleId scoped package', () => {
const m = util.getModuleId('@ionic/core/dist/ionic/css/ionic.css');
expect(m.moduleId).toBe('@ionic/core');
expect(m.filePath).toBe('dist/ionic/css/ionic.css');
});
it('getModuleId scoped package, no path', () => {
const m = util.getModuleId('@ionic/core');
expect(m.moduleId).toBe('@ionic/core');
expect(m.filePath).toBe('');
});
});