1 | const fse = require('fs-extra')
|
2 | const path = require('path')
|
3 | const remove = require('unist-util-remove')
|
4 | const select = require('unist-util-select')
|
5 | const test = require('ava')
|
6 | const {parse, parseFile, stringify} = require('..')
|
7 |
|
8 | const fixture = (...args) => path.resolve(__dirname, 'fixtures', ...args)
|
9 |
|
10 | test('it parses SCSS by default', t => {
|
11 | const root = parse('$foo: bar')
|
12 | t.deepEqual(root.type, 'stylesheet')
|
13 | t.deepEqual(root.children.length, 1)
|
14 | t.deepEqual(root.children[0].type, 'declaration')
|
15 | })
|
16 |
|
17 | test('it parses lists', t => {
|
18 | const root = parse('$list: (bar, baz)')
|
19 | t.is(root.children.length, 1)
|
20 | const [decl] = root.children
|
21 | t.is(decl.children.length, 4)
|
22 | const [name, colon, space, value] = decl.children
|
23 | const list = value.children[0]
|
24 | t.is(list.type, 'list')
|
25 | t.is(list.values.length, 2)
|
26 | t.deepEqual(list.values.map(stringify), ['bar', 'baz'])
|
27 | })
|
28 |
|
29 | test('it parses maps', t => {
|
30 | const root = parse('$list: (a: b, c: d)')
|
31 | t.is(root.children.length, 1)
|
32 | const [decl] = root.children
|
33 | t.is(decl.children.length, 4)
|
34 | const [name, colon, space, value] = decl.children
|
35 | const map = value.children[0]
|
36 | t.is(map.type, 'map')
|
37 | t.is(map.values.length, 2)
|
38 | t.deepEqual(
|
39 | map.values.map(({name, value}) => ({
|
40 | name: stringify(name),
|
41 | value: stringify(value),
|
42 | })),
|
43 | [
|
44 | {name: 'a', value: 'b'},
|
45 | {name: 'c', value: 'd'},
|
46 | ]
|
47 | )
|
48 | })
|
49 |
|
50 | test('it parses files', t => {
|
51 | return parseFile(fixture('basic.scss')).then(tree => {
|
52 | t.is(tree.type, 'stylesheet')
|
53 | })
|
54 | })
|
55 |
|
56 | test('it remembers the source string', t => {
|
57 | const file = fixture('basic.scss')
|
58 | return fse.readFile(file, 'utf8').then(source => {
|
59 | return parseFile(file).then(tree => {
|
60 | t.is(tree.source.string, source)
|
61 | t.falsy(Object.keys(tree.source).includes('string'),
|
62 | 'tree.source.string is enumerable')
|
63 | })
|
64 | })
|
65 | })
|
66 |
|
67 | test('it remembers the source path', t => {
|
68 | const file = fixture('basic.scss')
|
69 | return parseFile(file).then(tree => {
|
70 | t.is(tree.source.path, file)
|
71 | t.falsy(Object.keys(tree.source).includes('path'),
|
72 | 'tree.source.path is enumerable')
|
73 | })
|
74 | })
|
75 |
|
76 | test('it creates a tree that works with unist-util-select', t => {
|
77 | const tree = parse(`
|
78 | $color: red;
|
79 | a {
|
80 | color: $color;
|
81 | }
|
82 | `)
|
83 |
|
84 | remove(tree, 'space')
|
85 | t.is(select(tree, 'stylesheet')[0], tree)
|
86 | const firstVariable = tree.children[0].children[0].children[0]
|
87 | t.is(select(tree, 'declaration variable')[0], firstVariable)
|
88 | })
|
89 |
|
90 | test('it adds names to property nodes', t => {
|
91 | const tree = parse('a { color: green; }', {syntax: 'css'})
|
92 | const [property] = select(tree, 'property')
|
93 | t.is(property.name, 'color', 'property lacks name "color"')
|
94 | const selected = select(tree, 'property[name=color]')
|
95 | t.deepEqual(selected, [property])
|
96 | })
|