|
| 1 | +/* eslint max-len: ["error", 120, 2] */ |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | +const css = require('css'); |
| 5 | +const addRule = require('../src/core/addRule'); |
| 6 | + |
| 7 | +describe('addRule()', () => { |
| 8 | + |
| 9 | + // -------------------------- |
| 10 | + |
| 11 | + it('Should return a CSS with one rule added.', () => { |
| 12 | + |
| 13 | + const ast = css.parse(` |
| 14 | + .a { |
| 15 | + width: 10px; |
| 16 | + } |
| 17 | +
|
| 18 | + .b { |
| 19 | + color: #000; |
| 20 | + } |
| 21 | + `); |
| 22 | + |
| 23 | + // Add removeDeclaration method on Object |
| 24 | + addRule(ast); |
| 25 | + |
| 26 | + const newRule = css.parse(` |
| 27 | + .foo { |
| 28 | + height: 40px; |
| 29 | + } |
| 30 | + `); |
| 31 | + |
| 32 | + ast.addRule(newRule, 0); |
| 33 | + |
| 34 | + const result = css.stringify(ast); |
| 35 | + const expect = '.foo {\n height: 40px;\n}\n\n.a {\n width: 10px;\n}\n\n.b {\n color: #000;\n}'; |
| 36 | + assert.equal(result, expect); |
| 37 | + |
| 38 | + }); |
| 39 | + |
| 40 | + // -------------------------- |
| 41 | + |
| 42 | + it('Should return a CSS with one rule added on position 1.', () => { |
| 43 | + |
| 44 | + const ast = css.parse(` |
| 45 | + .a { |
| 46 | + width: 10px; |
| 47 | + } |
| 48 | +
|
| 49 | + .b { |
| 50 | + color: #000; |
| 51 | + } |
| 52 | + `); |
| 53 | + |
| 54 | + // Add removeDeclaration method on Object |
| 55 | + addRule(ast); |
| 56 | + |
| 57 | + const newRule = css.parse(` |
| 58 | + .foo { |
| 59 | + height: 40px; |
| 60 | + } |
| 61 | + `); |
| 62 | + |
| 63 | + ast.addRule(newRule, 1); |
| 64 | + |
| 65 | + const result = css.stringify(ast); |
| 66 | + const expect = '.a {\n width: 10px;\n}\n\n.foo {\n height: 40px;\n}\n\n.b {\n color: #000;\n}'; |
| 67 | + assert.equal(result, expect); |
| 68 | + |
| 69 | + }); |
| 70 | + |
| 71 | +}); |
0 commit comments