|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { compile, Middleware, middleware, prefixer, serialize, stringify } from 'stylis'; |
| 3 | +import muiRtlPlugin from '@mui/plugin-rtl'; |
| 4 | + |
| 5 | +const stylis = (css: string, extraPlugins: Middleware[] = []) => |
| 6 | + serialize(compile(css), middleware([...extraPlugins, muiRtlPlugin, stringify])); |
| 7 | + |
| 8 | +describe('integration test with stylis', () => { |
| 9 | + it('flips simple rules', () => { |
| 10 | + expect( |
| 11 | + stylis( |
| 12 | + `.a { |
| 13 | + padding-left: 5px; |
| 14 | + margin-right: 5px; |
| 15 | + border-left: 1px solid red; |
| 16 | + } |
| 17 | + `, |
| 18 | + ), |
| 19 | + ).to.equal(`.a{padding-right:5px;margin-left:5px;border-right:1px solid red;}`); |
| 20 | + }); |
| 21 | + |
| 22 | + it('flips shorthands', () => { |
| 23 | + expect( |
| 24 | + stylis( |
| 25 | + `.a { |
| 26 | + padding: 0 5px 0 0; |
| 27 | + margin: 0 0 0 5px; |
| 28 | + } |
| 29 | + `, |
| 30 | + ), |
| 31 | + ).to.equal(`.a{padding:0 0 0 5px;margin:0 5px 0 0;}`); |
| 32 | + }); |
| 33 | + |
| 34 | + it('handles noflip directives', () => { |
| 35 | + expect( |
| 36 | + stylis( |
| 37 | + ` |
| 38 | + .a { |
| 39 | + /* @noflip */ |
| 40 | + padding: 0 5px 0 0; |
| 41 | + margin: 0 0 0 5px; |
| 42 | + } |
| 43 | + `, |
| 44 | + ), |
| 45 | + ).to.equal(`.a{padding:0 5px 0 0;margin:0 5px 0 0;}`); |
| 46 | + }); |
| 47 | + |
| 48 | + it('flips keyframes', () => { |
| 49 | + expect( |
| 50 | + stylis( |
| 51 | + `@keyframes a { |
| 52 | + 0% { left: 0px; } |
| 53 | + 100% { left: 100px; } |
| 54 | + } |
| 55 | + `, |
| 56 | + ), |
| 57 | + ).to.equal(`@keyframes a{0%{right:0px;}100%{right:100px;}}`); |
| 58 | + }); |
| 59 | + |
| 60 | + it('flips media queries', () => { |
| 61 | + expect( |
| 62 | + stylis( |
| 63 | + `@media (min-width: 500px) { |
| 64 | + .a { |
| 65 | + padding-left: 5px; |
| 66 | + margin-right: 5px; |
| 67 | + border-left: 1px solid red; |
| 68 | + } |
| 69 | + } |
| 70 | + `, |
| 71 | + ), |
| 72 | + ).to.equal( |
| 73 | + `@media (min-width: 500px){.a{padding-right:5px;margin-left:5px;border-right:1px solid red;}}`, |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it('flips supports queries', () => { |
| 78 | + expect( |
| 79 | + stylis( |
| 80 | + `@supports (display: flex) { |
| 81 | + .a { |
| 82 | + padding-left: 5px; |
| 83 | + margin-right: 5px; |
| 84 | + border-left: 1px solid red; |
| 85 | + } |
| 86 | + } |
| 87 | + `, |
| 88 | + ), |
| 89 | + ).to.equal( |
| 90 | + `@supports (display: flex){.a{padding-right:5px;margin-left:5px;border-right:1px solid red;}}`, |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it('works in tandem with prefixer', () => { |
| 95 | + expect( |
| 96 | + stylis( |
| 97 | + `@keyframes a { |
| 98 | + 0% { left: 0px; } |
| 99 | + 100% { left: 100px; } |
| 100 | + } |
| 101 | + `, |
| 102 | + [prefixer], |
| 103 | + ), |
| 104 | + ).to.equal( |
| 105 | + `@-webkit-keyframes a{0%{right:0px;}100%{right:100px;}}@keyframes a{0%{right:0px;}100%{right:100px;}}`, |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it("doesn't crash on empty rules", () => { |
| 110 | + // this generates nodes for: |
| 111 | + // .cls{} |
| 112 | + // .cls .nested{color:hotpink;} |
| 113 | + expect( |
| 114 | + stylis(` |
| 115 | + .cls { |
| 116 | + & .nested { |
| 117 | + color:hotpink; |
| 118 | + } |
| 119 | + } |
| 120 | + `), |
| 121 | + ).to.equal(`.cls .nested{color:hotpink;}`); |
| 122 | + }); |
| 123 | + |
| 124 | + it('works for nested rules', () => { |
| 125 | + expect( |
| 126 | + stylis(` |
| 127 | + .cls { |
| 128 | + margin-right: 32px; |
| 129 | + & .first-child { |
| 130 | + margin-right: 32px; |
| 131 | + } |
| 132 | + } |
| 133 | + `), |
| 134 | + ).to.equal(`.cls{margin-left:32px;}.cls .first-child{margin-left:32px;}`); |
| 135 | + }); |
| 136 | + |
| 137 | + it('works for layer rules', () => { |
| 138 | + expect( |
| 139 | + stylis(` |
| 140 | + @layer default { |
| 141 | + .cls { |
| 142 | + margin-right: 32px; |
| 143 | + & .first-child { |
| 144 | + margin-right: 32px; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + `), |
| 149 | + ).to.equal(`@layer default{.cls{margin-left:32px;}.cls .first-child{margin-left:32px;}}`); |
| 150 | + }); |
| 151 | + |
| 152 | + it('works for nested layer rules', () => { |
| 153 | + expect( |
| 154 | + stylis(` |
| 155 | + @layer root { |
| 156 | + .foo { |
| 157 | + margin-right: 32px; |
| 158 | + } |
| 159 | + @layer default { |
| 160 | + .cls { |
| 161 | + margin-right: 32px; |
| 162 | + & .first-child { |
| 163 | + margin-right: 32px; |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + `), |
| 169 | + ).to.equal( |
| 170 | + `@layer root{.foo{margin-left:32px;}@layer default{.cls{margin-left:32px;}.cls .first-child{margin-left:32px;}}}`, |
| 171 | + ); |
| 172 | + }); |
| 173 | +}); |
0 commit comments