source: trip-planner-front/node_modules/postcss-selector-parser/dist/__tests__/parser.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 7.1 KB
Line 
1"use strict";
2
3var _ava = _interopRequireDefault(require("ava"));
4
5var _index = _interopRequireDefault(require("../index"));
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
8
9// Node creation
10var nodeTypes = [['attribute', '[href]', {
11 attribute: 'href'
12}], ['className', '.classy', {
13 value: 'classy'
14}], ['combinator', ' >> ', {
15 value: '>>',
16 spaces: {
17 before: ' ',
18 after: ' '
19 }
20}], ['comment', '/* comment */', {
21 value: '/* comment */'
22}], ['id', '#test', {
23 value: 'test'
24}], ['nesting', '&'], ['pseudo', '::before', {
25 value: '::before'
26}], ['string', '"wow"', {
27 value: '"wow"'
28}], ['tag', 'button', {
29 value: 'button'
30}], ['universal', '*']];
31nodeTypes.forEach(function (type) {
32 (0, _ava["default"])("parser#" + type[0], function (t) {
33 var node = _index["default"][type[0]](type[2] || {});
34
35 t.deepEqual(String(node), type[1]);
36 });
37});
38(0, _ava["default"])('string constants', function (t) {
39 t.truthy(_index["default"].TAG);
40 t.truthy(_index["default"].STRING);
41 t.truthy(_index["default"].SELECTOR);
42 t.truthy(_index["default"].ROOT);
43 t.truthy(_index["default"].PSEUDO);
44 t.truthy(_index["default"].NESTING);
45 t.truthy(_index["default"].ID);
46 t.truthy(_index["default"].COMMENT);
47 t.truthy(_index["default"].COMBINATOR);
48 t.truthy(_index["default"].CLASS);
49 t.truthy(_index["default"].ATTRIBUTE);
50 t.truthy(_index["default"].UNIVERSAL);
51});
52(0, _ava["default"])('construct a whole tree', function (t) {
53 var root = _index["default"].root();
54
55 var selector = _index["default"].selector();
56
57 selector.append(_index["default"].id({
58 value: 'tree'
59 }));
60 root.append(selector);
61 t.deepEqual(String(root), '#tree');
62});
63(0, _ava["default"])('no operation', function (t) {
64 t.notThrows(function () {
65 return (0, _index["default"])().processSync('h1 h2 h3');
66 });
67});
68(0, _ava["default"])('empty selector string', function (t) {
69 t.notThrows(function () {
70 return (0, _index["default"])(function (selectors) {
71 selectors.walk(function (selector) {
72 selector.type = 'tag';
73 });
74 }).processSync('');
75 });
76});
77(0, _ava["default"])('async parser', function (t) {
78 return (0, _index["default"])(function (selectors) {
79 return new Promise(function (res) {
80 setTimeout(function () {
81 selectors.first.nodes[0].value = 'bar';
82 res();
83 }, 1);
84 });
85 }).process('foo').then(function (result) {
86 t.deepEqual(result, 'bar');
87 });
88});
89(0, _ava["default"])('parse errors with the async parser', function (t) {
90 return (0, _index["default"])(function (selectors) {
91 return new Promise(function (res) {
92 setTimeout(function () {
93 selectors.first.nodes[0].value = 'bar';
94 res();
95 }, 1);
96 });
97 }).process('a b: c')["catch"](function (err) {
98 return t.truthy(err);
99 });
100});
101(0, _ava["default"])('parse errors within the async processor', function (t) {
102 return (0, _index["default"])(function (selectors) {
103 return new Promise(function (res, rej) {
104 setTimeout(function () {
105 rej(selectors.error("async error"));
106 }, 1);
107 });
108 }).process('.foo')["catch"](function (err) {
109 return t.truthy(err);
110 });
111});
112(0, _ava["default"])('parse errors within the async processor before the promise returns', function (t) {
113 return (0, _index["default"])(function (selectors) {
114 throw selectors.error("async error");
115 }).process('.foo')["catch"](function (err) {
116 return t.truthy(err);
117 });
118});
119(0, _ava["default"])('returning a promise to the sync processor fails', function (t) {
120 t["throws"](function () {
121 return (0, _index["default"])(function () {
122 return new Promise(function (res) {
123 setTimeout(function () {
124 res();
125 }, 1);
126 });
127 }).processSync('.foo');
128 });
129});
130(0, _ava["default"])('Passing a rule works async', function (t) {
131 var rule = {
132 selector: '.foo'
133 };
134 return (0, _index["default"])(function (root) {
135 return new Promise(function (res) {
136 setTimeout(function () {
137 root.walkClasses(function (node) {
138 node.value = "bar";
139 });
140 res();
141 }, 1);
142 });
143 }).process(rule).then(function (newSel) {
144 t.deepEqual(newSel, ".bar");
145 t.deepEqual(rule.selector, ".bar");
146 });
147});
148(0, _ava["default"])('Passing a rule with mutation disabled works async', function (t) {
149 var rule = {
150 selector: '.foo'
151 };
152 return (0, _index["default"])(function (root) {
153 return new Promise(function (res) {
154 setTimeout(function () {
155 root.walkClasses(function (node) {
156 node.value = "bar";
157 });
158 res();
159 }, 1);
160 });
161 }).process(rule, {
162 updateSelector: false
163 }).then(function (newSel) {
164 t.deepEqual(newSel, ".bar");
165 t.deepEqual(rule.selector, ".foo");
166 });
167});
168(0, _ava["default"])('Passing a rule with mutation works sync', function (t) {
169 var rule = {
170 selector: '.foo'
171 };
172 var newSel = (0, _index["default"])(function (root) {
173 root.walkClasses(function (node) {
174 node.value = "bar";
175 });
176 }).processSync(rule, {
177 updateSelector: true
178 });
179 t.deepEqual(newSel, ".bar");
180 t.deepEqual(rule.selector, ".bar");
181});
182(0, _ava["default"])('Transform a selector synchronously', function (t) {
183 var rule = {
184 selector: '.foo'
185 };
186 var count = (0, _index["default"])(function (root) {
187 var classCount = 0;
188 root.walkClasses(function (node) {
189 classCount++;
190 node.value = "bar";
191 });
192 return classCount;
193 }).transformSync(rule, {
194 updateSelector: true
195 });
196 t.deepEqual(count, 1);
197 t.deepEqual(rule.selector, ".bar");
198});
199(0, _ava["default"])('Transform a selector asynchronously', function (t) {
200 var rule = {
201 selector: '.foo'
202 };
203 return (0, _index["default"])(function (root) {
204 return new Promise(function (res) {
205 setTimeout(function () {
206 var classCount = 0;
207 root.walkClasses(function (node) {
208 classCount++;
209 node.value = "bar";
210 });
211 res(classCount);
212 }, 1);
213 });
214 }).transform(rule, {
215 updateSelector: true
216 }).then(function (count) {
217 t.deepEqual(count, 1);
218 t.deepEqual(rule.selector, ".bar");
219 });
220});
221(0, _ava["default"])('get AST of a selector synchronously', function (t) {
222 var rule = {
223 selector: '.foo'
224 };
225 var ast = (0, _index["default"])(function (root) {
226 var classCount = 0;
227 root.walkClasses(function (node) {
228 classCount++;
229 node.value = "bar";
230 });
231 return classCount;
232 }).astSync(rule, {
233 updateSelector: true
234 });
235 t.deepEqual(ast.nodes[0].nodes[0].value, "bar");
236 t.deepEqual(rule.selector, ".bar");
237});
238(0, _ava["default"])('get AST a selector asynchronously', function (t) {
239 var rule = {
240 selector: '.foo'
241 };
242 return (0, _index["default"])(function (root) {
243 return new Promise(function (res) {
244 setTimeout(function () {
245 var classCount = 0;
246 root.walkClasses(function (node) {
247 classCount++;
248 node.value = "bar";
249 });
250 res(classCount);
251 }, 1);
252 });
253 }).ast(rule, {
254 updateSelector: true
255 }).then(function (ast) {
256 t.deepEqual(ast.nodes[0].nodes[0].value, "bar");
257 t.deepEqual(rule.selector, ".bar");
258 });
259});
Note: See TracBrowser for help on using the repository browser.