|
| 1 | +let postcss = require('postcss') |
| 2 | +let postcssNested = require('postcss-nested') |
| 3 | +let plugin = require('.') |
| 4 | + |
| 5 | +it('should be possible to load a custom nesting plugin', async () => { |
| 6 | + let input = css` |
| 7 | + .foo { |
| 8 | + color: black; |
| 9 | + @screen md { |
| 10 | + color: blue; |
| 11 | + } |
| 12 | + } |
| 13 | + ` |
| 14 | + |
| 15 | + expect( |
| 16 | + await run(input, function (root) { |
| 17 | + root.walkRules((rule) => { |
| 18 | + rule.selector += '-modified' |
| 19 | + }) |
| 20 | + }) |
| 21 | + ).toMatchCss(css` |
| 22 | + .foo-modified { |
| 23 | + color: black; |
| 24 | + @media screen(md) { |
| 25 | + color: blue; |
| 26 | + } |
| 27 | + } |
| 28 | + `) |
| 29 | +}) |
| 30 | + |
| 31 | +it('should be possible to load a custom nesting plugin by name (string) instead', async () => { |
| 32 | + let input = css` |
| 33 | + .foo { |
| 34 | + color: black; |
| 35 | + @screen md { |
| 36 | + color: blue; |
| 37 | + } |
| 38 | + } |
| 39 | + ` |
| 40 | + |
| 41 | + expect(await run(input, 'postcss-nested')).toMatchCss(css` |
| 42 | + .foo { |
| 43 | + color: black; |
| 44 | + } |
| 45 | +
|
| 46 | + @media screen(md) { |
| 47 | + .foo { |
| 48 | + color: blue; |
| 49 | + } |
| 50 | + } |
| 51 | + `) |
| 52 | +}) |
| 53 | + |
| 54 | +it('should default to the bundled postcss-nested plugin (no options)', async () => { |
| 55 | + let input = css` |
| 56 | + .foo { |
| 57 | + color: black; |
| 58 | + @screen md { |
| 59 | + color: blue; |
| 60 | + } |
| 61 | + } |
| 62 | + ` |
| 63 | + |
| 64 | + expect(await run(input)).toMatchCss(css` |
| 65 | + .foo { |
| 66 | + color: black; |
| 67 | + } |
| 68 | +
|
| 69 | + @media screen(md) { |
| 70 | + .foo { |
| 71 | + color: blue; |
| 72 | + } |
| 73 | + } |
| 74 | + `) |
| 75 | +}) |
| 76 | + |
| 77 | +it('should default to the bundled postcss-nested plugin (empty ooptions)', async () => { |
| 78 | + let input = css` |
| 79 | + .foo { |
| 80 | + color: black; |
| 81 | + @screen md { |
| 82 | + color: blue; |
| 83 | + } |
| 84 | + } |
| 85 | + ` |
| 86 | + |
| 87 | + expect(await run(input, {})).toMatchCss(css` |
| 88 | + .foo { |
| 89 | + color: black; |
| 90 | + } |
| 91 | +
|
| 92 | + @media screen(md) { |
| 93 | + .foo { |
| 94 | + color: blue; |
| 95 | + } |
| 96 | + } |
| 97 | + `) |
| 98 | +}) |
| 99 | + |
| 100 | +test('@screen rules are replaced with media queries', async () => { |
| 101 | + let input = css` |
| 102 | + .foo { |
| 103 | + color: black; |
| 104 | + @screen md { |
| 105 | + color: blue; |
| 106 | + } |
| 107 | + } |
| 108 | + ` |
| 109 | + |
| 110 | + expect(await run(input, postcssNested)).toMatchCss(css` |
| 111 | + .foo { |
| 112 | + color: black; |
| 113 | + } |
| 114 | +
|
| 115 | + @media screen(md) { |
| 116 | + .foo { |
| 117 | + color: blue; |
| 118 | + } |
| 119 | + } |
| 120 | + `) |
| 121 | +}) |
| 122 | + |
| 123 | +test('@screen rules can work with `@apply`', async () => { |
| 124 | + let input = css` |
| 125 | + .foo { |
| 126 | + @apply bg-black; |
| 127 | + @screen md { |
| 128 | + @apply bg-blue-500; |
| 129 | + } |
| 130 | + } |
| 131 | + ` |
| 132 | + |
| 133 | + expect(await run(input, postcssNested)).toMatchCss(css` |
| 134 | + .foo { |
| 135 | + @apply bg-black; |
| 136 | + } |
| 137 | +
|
| 138 | + @media screen(md) { |
| 139 | + .foo { |
| 140 | + @apply bg-blue-500; |
| 141 | + } |
| 142 | + } |
| 143 | + `) |
| 144 | +}) |
| 145 | + |
| 146 | +// --- |
| 147 | + |
| 148 | +function indentRecursive(node, indent = 0) { |
| 149 | + node.each && |
| 150 | + node.each((child, i) => { |
| 151 | + if (!child.raws.before || child.raws.before.includes('\n')) { |
| 152 | + child.raws.before = `\n${node.type !== 'rule' && i > 0 ? '\n' : ''}${' '.repeat(indent)}` |
| 153 | + } |
| 154 | + child.raws.after = `\n${' '.repeat(indent)}` |
| 155 | + indentRecursive(child, indent + 1) |
| 156 | + }) |
| 157 | +} |
| 158 | + |
| 159 | +function formatNodes(root) { |
| 160 | + indentRecursive(root) |
| 161 | + if (root.first) { |
| 162 | + root.first.raws.before = '' |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +async function run(input, options) { |
| 167 | + return ( |
| 168 | + await postcss([options === undefined ? plugin : plugin(options), formatNodes]).process(input, { |
| 169 | + from: undefined, |
| 170 | + }) |
| 171 | + ).toString() |
| 172 | +} |
| 173 | + |
| 174 | +function css(templates) { |
| 175 | + return templates.join('') |
| 176 | +} |
0 commit comments