-
Notifications
You must be signed in to change notification settings - Fork 17
/
test.js
173 lines (153 loc) · 6.15 KB
/
test.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
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
/* eslint max-len: 0 */
const expect = require('chai').expect;
const path = require('path');
const sass = require('node-sass');
const moduleImporter = require('./lib');
function getCSS(file, data) {
return new Promise((resolve, reject) => {
sass.render({
data,
file: file ? `./fixtures/${file}` : null,
outputStyle: 'compressed',
includePaths: [path.join(__dirname, 'fixtures')],
importer: moduleImporter({
basedir: path.join(__dirname, 'fixtures'),
}),
}, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res.css.toString());
}
});
});
}
describe('sass-module-importer', () => {
it('should import a local file', (done) => {
getCSS(null, '@import "dummy";').then((css) => {
const expected = 'body{content:"local"}\n';
expect(css).to.exist.and.equal(expected);
done();
});
});
it('should import nested files and dependencies', (done) => {
getCSS(null, '@import "test-npm-nested";').then((css) => {
const expected = `*,*:after,*:before{box-sizing:border-box}html,body{margin:0;padding:0}.test{content:"SCSS from 'npm' and from 'style' field."}.child{content:'I am being imported by another file'}.test{content:"SCSS from 'npm' and from 'main' field."}\n`;
expect(css).to.exist.and.equal(expected);
done();
});
});
it('should fail to import non-existing module', (done) => {
getCSS(null, '@import "unicorn";').catch((err) => {
const expected = {
message: 'File to import not found or unreadable: unicorn\nParent style sheet: stdin',
};
expect(err.message).to.exist.and.equal(expected.message);
done();
});
});
describe('npm', () => {
it('should import contents of CSS from npm module using the "main" field', (done) => {
getCSS(null, '@import "test-npm-main-css";').then((css) => {
const expected = `.test{content:"CSS from 'npm' and from 'main' field."}\n`;
expect(css).to.exist.and.equal(expected);
done();
});
});
it('should import SCSS from npm module using the "main" field', (done) => {
getCSS(null, '@import "test-npm-main-scss";').then((css) => {
const expected = `.test{content:"SCSS from 'npm' and from 'main' field."}\n`;
expect(css).to.exist.and.equal(expected);
done();
});
});
it('should import contents of CSS from npm module using the "style" field', (done) => {
getCSS(null, '@import "test-npm-style-css";').then((css) => {
const expected = `.test{content:"CSS from 'npm' and from 'style' field."}\n`;
expect(css).to.exist.and.equal(expected);
done();
});
});
it('should import SCSS from npm module using the "style" field', (done) => {
getCSS(null, '@import "test-npm-style-scss";').then((css) => {
const expected = `.test{content:"SCSS from 'npm' and from 'style' field."}\n`;
expect(css).to.exist.and.equal(expected);
done();
});
});
it('should import "index.css" if the "main" and "style" are undefined', (done) => {
getCSS(null, '@import "test-npm-index-css";').then((css) => {
const expected = `.test{content:"CSS from 'npm' as index.css fallback."}\n`;
expect(css).to.exist.and.equal(expected);
done();
});
});
it('should import a partial from a npm module', (done) => {
getCSS(null, '@import "test-normalize/normalize/_body.scss";').then((css) => {
const expected = 'html,body{margin:0;padding:0}\n';
expect(css).to.exist.and.equal(expected);
done();
});
});
});
describe('bower', () => {
it('should import contents of CSS from bower module using the "main" field', (done) => {
getCSS(null, '@import "test-bower-main-css";').then((css) => {
const expected = `.test{content:"CSS from 'bower' and from 'main' field."}\n`;
expect(css).to.exist.and.equal(expected);
done();
});
});
it('should import SCSS from bower module using the "main" field', (done) => {
getCSS(null, '@import "test-bower-main-scss";').then((css) => {
const expected = `.test{content:"SCSS from 'bower' and from 'main' field."}\n`;
expect(css).to.exist.and.equal(expected);
done();
});
});
it('should import contents of CSS from bower module using the "style" field', (done) => {
getCSS(null, '@import "test-bower-style-css";').then((css) => {
const expected = `.test{content:"CSS from 'bower' and from 'style' field."}\n`;
expect(css).to.exist.and.equal(expected);
done();
});
});
it('should import SCSS from bower module using the "style" field', (done) => {
getCSS(null, '@import "test-bower-style-scss";').then((css) => {
const expected = `.test{content:"SCSS from 'bower' and from 'style' field."}\n`;
expect(css).to.exist.and.equal(expected);
done();
});
});
it('should import "index.css" if the "main" and "style" are undefined', (done) => {
getCSS(null, '@import "test-bower-index-css";').then((css) => {
const expected = `.test{content:"CSS from 'bower' as index.css fallback."}\n`;
expect(css).to.exist.and.equal(expected);
done();
});
});
it('should import the first style file from the "main" option if it is an object', (done) => {
getCSS(null, '@import "test-bower-main-array";').then((css) => {
const expected = `.test{content:"CSS from first file in 'main' array"}\n`;
expect(css).to.exist.and.equal(expected);
done();
});
});
});
describe('module vs local file', () => {
it('should import external colors module', (done) => {
getCSS(null, '@import "colors";').then((css) => {
const expected = '.colors{content:"Colors library"}\n';
expect(css).to.exist.and.equal(expected);
done();
});
});
it('should import local colors if using relative path', (done) => {
getCSS(null, '@import "./colors";').then((css) => {
const expected = '.red{color:red}.green{color:green}\n';
expect(css).to.exist.and.equal(expected);
done();
});
});
});
});