Skip to content

Commit 16c0c50

Browse files
committed
Simplify test
1 parent c00ad09 commit 16c0c50

1 file changed

Lines changed: 49 additions & 45 deletions

File tree

test/visitor.test.ts

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,50 @@ function addIndex (array: any[][]) {
2525
})
2626
}
2727

28+
function buildVisitor (): [[string, string][], Plugin] {
29+
let visits: [string, string][] = []
30+
let visitor: Plugin = {
31+
postcssPlugin: 'visitor',
32+
Once (i) {
33+
visits.push(['Once', `${i.nodes.length}`])
34+
},
35+
Root (i) {
36+
visits.push(['Root', `${i.nodes.length}`])
37+
},
38+
RootExit (i) {
39+
visits.push(['RootExit', `${i.nodes.length}`])
40+
},
41+
AtRule (i) {
42+
visits.push(['AtRule', i.name])
43+
},
44+
AtRuleExit (i) {
45+
visits.push(['AtRuleExit', i.name])
46+
},
47+
Rule (i) {
48+
visits.push(['Rule', i.selector])
49+
},
50+
RuleExit (i) {
51+
visits.push(['RuleExit', i.selector])
52+
},
53+
Declaration (i) {
54+
visits.push(['Declaration', i.prop + ': ' + i.value])
55+
},
56+
DeclarationExit (i) {
57+
visits.push(['DeclarationExit', i.prop + ': ' + i.value])
58+
},
59+
Comment (i) {
60+
visits.push(['Comment', i.text])
61+
},
62+
CommentExit (i) {
63+
visits.push(['CommentExit', i.text])
64+
},
65+
OnceExit (i) {
66+
visits.push(['OnceExit', `${i.nodes.length}`])
67+
}
68+
}
69+
return [visits, visitor]
70+
}
71+
2872
let replaceColorGreenClassic: Plugin = {
2973
postcssPlugin: 'replace-color',
3074
Once (root) {
@@ -594,47 +638,6 @@ it('works correctly with nodes changes', () => {
594638
).toEqual('a{ z-index: 1; color: black }')
595639
})
596640

597-
let visits: [string, string][] = []
598-
let visitor: Plugin = {
599-
postcssPlugin: 'visitor',
600-
Once (i) {
601-
visits.push(['Once', `${i.nodes.length}`])
602-
},
603-
Root (i) {
604-
visits.push(['Root', `${i.nodes.length}`])
605-
},
606-
RootExit (i) {
607-
visits.push(['RootExit', `${i.nodes.length}`])
608-
},
609-
AtRule (i) {
610-
visits.push(['AtRule', i.name])
611-
},
612-
AtRuleExit (i) {
613-
visits.push(['AtRuleExit', i.name])
614-
},
615-
Rule (i) {
616-
visits.push(['Rule', i.selector])
617-
},
618-
RuleExit (i) {
619-
visits.push(['RuleExit', i.selector])
620-
},
621-
Declaration (i) {
622-
visits.push(['Declaration', i.prop + ': ' + i.value])
623-
},
624-
DeclarationExit (i) {
625-
visits.push(['DeclarationExit', i.prop + ': ' + i.value])
626-
},
627-
Comment (i) {
628-
visits.push(['Comment', i.text])
629-
},
630-
CommentExit (i) {
631-
visits.push(['CommentExit', i.text])
632-
},
633-
OnceExit (i) {
634-
visits.push(['OnceExit', `${i.nodes.length}`])
635-
}
636-
}
637-
638641
const redToGreen: Plugin = {
639642
postcssPlugin: 'redToGreen',
640643
Declaration: {
@@ -694,7 +697,7 @@ const insertFirst: Plugin = {
694697

695698
for (let type of ['sync', 'async']) {
696699
it(`walks ${type} through tree`, async () => {
697-
visits = []
700+
let [visits, visitor] = buildVisitor()
698701
let processor = postcss([visitor]).process(
699702
`@media screen {
700703
body {
@@ -738,7 +741,7 @@ for (let type of ['sync', 'async']) {
738741
})
739742

740743
it(`walks ${type} during transformations`, async () => {
741-
visits = []
744+
let [visits, visitor] = buildVisitor()
742745
let result = postcss([
743746
visitor,
744747
redToGreen,
@@ -962,9 +965,10 @@ it('throws error from async OnceExit', async () => {
962965
})
963966

964967
it('rescan Root in another processor', () => {
968+
let [visits, visitor] = buildVisitor()
965969
let root = postcss([visitor]).process('a{z-index:1}', { from: 'a.css' }).root
966970

967-
visits = []
971+
visits.splice(0, visits.length)
968972
postcss([visitor]).process(root, { from: 'a.css' }).root
969973

970974
expect(visits).toEqual([
@@ -990,7 +994,7 @@ it('marks cleaned nodes as dirty on moving', () => {
990994
}
991995
}
992996

993-
visits = []
997+
let [visits, visitor] = buildVisitor()
994998
postcss([mover, visitor]).process('a { color: black } b { }', {
995999
from: 'a.css'
9961000
}).root

0 commit comments

Comments
 (0)