This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
187 lines (158 loc) · 5.12 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* eslint no-undef: 0 */
'use strict';
const expect = require('chai').expect;
const Plugin = require('./');
const fs = require('fs');
const sysPath = require('path');
const fixturesPath = sysPath.resolve(__dirname, 'fixtures');
describe('Plugin', () => {
let plugin;
const path = 'fixtures/app/styles/style.styl';
beforeEach(() => {
plugin = new Plugin({
paths: {
root: '',
},
plugins: {
stylus: {
paths: [fixturesPath],
defines: {
url: require('stylus').url(),
},
},
},
});
});
it('should be an object', () => {
expect(plugin).to.be.an('object');
});
describe('#compile', () => {
it('should have #compile method', () => {
expect(plugin).to.respondTo('compile');
});
it('should compile and produce valid result', () => {
const imagePath = './dot.jpg';
const base64 = fs.readFileSync(`${fixturesPath}/${imagePath}`).toString('base64');
const data = `body\n font: 12px Helvetica, Arial, sans-serif\n background: url("${imagePath}")`;
const expected = `body {\n font: 12px Helvetica, Arial, sans-serif;\n background: url("data:image/jpeg;base64,${base64}");\n}\n`;
return plugin.compile({data, path}).then(result => {
expect(result.data).to.equal(expected);
});
});
it('should compile and import from config.stylus.paths', () => {
const data = "@import 'path_test'\n";
const expected = '.test {\n color: #fff;\n}\n';
return plugin.compile({data, path}).then(result => {
expect(result.data).to.equal(expected);
});
});
it('should throw error in correct format', () => {
const data = ">";
const expected = `L1:2 \n 1| >
-------^\n
expected "indent", got "eos"\n`;
return plugin.compile({data, path}).catch(error => {
expect(error.stack).to.be.a('string');
expect(error.toString()).to.equal(expected);
});
});
});
describe('getDependencies', () => {
it('should output valid deps', () => {
const data = `
@import unquoted
@import 'valid1'
@import '__--valid2--'
@import "./valid3.styl"
@import '../../vendor/styles/valid4'
@import 'nib'
// @import 'commented'
`;
const expected = [
sysPath.join('fixtures', 'app', 'styles', 'unquoted.styl'),
sysPath.join('fixtures', 'app', 'styles', 'valid1.styl'),
sysPath.join('fixtures', 'app', 'styles', '__--valid2--.styl'),
sysPath.join('fixtures', 'app', 'styles', 'valid3.styl'),
sysPath.join('fixtures', 'vendor', 'styles', 'valid4.styl'),
];
return plugin.getDependencies({data, path}).then(deps => {
expect(deps).to.deep.equal(expected);
});
});
it('should match globs', () => {
const data = '@import styles/*';
const path = 'fixtures/app/glob_test.styl';
const expected = [
sysPath.join('fixtures', 'app', 'styles', 'unquoted.styl'),
sysPath.join('fixtures', 'app', 'styles', 'valid1.styl'),
sysPath.join('fixtures', 'app', 'styles', '__--valid2--.styl'),
sysPath.join('fixtures', 'app', 'styles', 'valid3.styl'),
].sort();
return plugin.getDependencies({data, path}).then(deps => {
expect(deps.sort()).to.deep.equal(expected);
});
});
});
});
describe('Plugin Import Module', () => {
const path = 'fixtures/app/styles/style.styl';
const pluginPath = sysPath.resolve(__dirname, 'fixtures/plugin-import-module/index.js');
let plugin;
it('Add plugin import should add fn', () => {
// import add plugin
plugin = new Plugin({
paths: {
root: '',
},
plugins: {
stylus: {
plugins: [[pluginPath, 'add']],
},
},
});
const data = `body\n top: add(1, 3)`;
const expected = `body {\n top: 4;\n}\n`;
return plugin.compile({data, path}).then(result => {
expect(result.data).to.equal(expected);
});
});
it('Sub plugin import should sub fn', () => {
// import add plugin
plugin = new Plugin({
paths: {
root: '',
},
plugins: {
stylus: {
plugins: [[pluginPath, 'sub']],
},
},
});
const data = `body\n top: sub(3, 2)`;
const expected = `body {\n top: 1;\n}\n`;
return plugin.compile({data, path}).then(result => {
expect(result.data).to.equal(expected);
});
});
});
describe('Plugin as a function', () => {
const path = 'fixtures/app/styles/style.styl';
it('Compile results and should be prefixed', () => {
// import add plugin
const plugin = new Plugin({
paths: {
root: '',
},
plugins: {
stylus: {
plugins: [require('autoprefixer-stylus')({browsers: ['last 99 versions']})],
},
},
});
const data = `body\n display: flex`;
const expected = `body {\n display: -webkit-box;\n display: -webkit-flex;\n display: -moz-box;\n display: -ms-flexbox;\n display: flex;\n}\n`;
return plugin.compile({data, path}).then(result => {
expect(result.data).to.equal(expected);
});
});
});