|
| 1 | +const dedent = require('dedent') |
| 2 | +const stylelint = require('stylelint') |
| 3 | +const pluginPath = require.resolve('../plugins/borders') |
| 4 | + |
| 5 | +const ruleName = 'primer/borders' |
| 6 | +const configWithOptions = args => ({ |
| 7 | + plugins: [pluginPath], |
| 8 | + rules: { |
| 9 | + [ruleName]: args |
| 10 | + } |
| 11 | +}) |
| 12 | + |
| 13 | +describe(ruleName, () => { |
| 14 | + it('reports compound border properties', () => { |
| 15 | + return stylelint |
| 16 | + .lint({ |
| 17 | + code: ` |
| 18 | + .foo { border: $red; } |
| 19 | + `, |
| 20 | + config: configWithOptions(true) |
| 21 | + }) |
| 22 | + .then(data => { |
| 23 | + expect(data).toHaveErrored() |
| 24 | + expect(data).toHaveWarnings([`Please use "$border-red" instead of "$red". (${ruleName})`]) |
| 25 | + }) |
| 26 | + }) |
| 27 | + |
| 28 | + it('reports multiple border properties', () => { |
| 29 | + return stylelint |
| 30 | + .lint({ |
| 31 | + code: ` |
| 32 | + .foo { border: 1px solid gray; } |
| 33 | + `, |
| 34 | + config: configWithOptions(true) |
| 35 | + }) |
| 36 | + .then(data => { |
| 37 | + expect(data).toHaveErrored() |
| 38 | + expect(data).toHaveWarnings([ |
| 39 | + `Please use "$border-width" instead of "1px". (${ruleName})`, |
| 40 | + `Please use "$border-style" instead of "solid". (${ruleName})`, |
| 41 | + `Please use a border color variable instead of "gray". (${ruleName})` |
| 42 | + ]) |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + it('recognizes function calls as whole tokens', () => { |
| 47 | + return stylelint |
| 48 | + .lint({ |
| 49 | + code: ` |
| 50 | + .foo { border: calc($spacer-2 + var(--derp)) $border-style rgba($border-gray-dark, 50%); } |
| 51 | + `, |
| 52 | + config: configWithOptions(true) |
| 53 | + }) |
| 54 | + .then(data => { |
| 55 | + expect(data).toHaveErrored() |
| 56 | + expect(data).toHaveWarnings([ |
| 57 | + `Please use a border width variable instead of "calc($spacer-2 + var(--derp))". (${ruleName})`, |
| 58 | + `Please use a border color variable instead of "rgba($border-gray-dark, 50%)". (${ruleName})` |
| 59 | + ]) |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + it('allows $border shorthand in border{,-top,-right,-bottom,-left}', () => { |
| 64 | + return stylelint |
| 65 | + .lint({ |
| 66 | + code: dedent` |
| 67 | + .a { border: $border; } |
| 68 | + .b { border-top: $border; } |
| 69 | + .c { border-right: $border; } |
| 70 | + .d { border-bottom: $border; } |
| 71 | + .e { border-left: $border; } |
| 72 | + `, |
| 73 | + config: configWithOptions(true) |
| 74 | + }) |
| 75 | + .then(data => { |
| 76 | + expect(data).not.toHaveErrored() |
| 77 | + expect(data).toHaveWarningsLength(0) |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + describe('autofix', () => { |
| 82 | + it('fixes border variables', () => { |
| 83 | + return stylelint |
| 84 | + .lint({ |
| 85 | + code: dedent` |
| 86 | + .a { border: 1px solid $gray-300; } |
| 87 | + .b { border: 1px solid $gray-200; } |
| 88 | + .c { border: solid 1px $border-gray; } |
| 89 | + .d { border: 1px $border-color solid; } |
| 90 | + `, |
| 91 | + config: configWithOptions(true), |
| 92 | + fix: true |
| 93 | + }) |
| 94 | + .then(data => { |
| 95 | + expect(data).not.toHaveErrored() |
| 96 | + expect(data).toHaveWarningsLength(0) |
| 97 | + expect(data.output).toEqual(dedent` |
| 98 | + .a { border: $border-width $border-style $border-gray-dark; } |
| 99 | + .b { border: $border; } |
| 100 | + .c { border: $border; } |
| 101 | + .d { border: $border; } |
| 102 | + `) |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + it('fixes border radius', () => { |
| 107 | + return stylelint |
| 108 | + .lint({ |
| 109 | + code: dedent` |
| 110 | + .x { border-radius: 3px; } |
| 111 | + .y { |
| 112 | + border-top-left-radius: 3px; |
| 113 | + border-bottom-left-radius: 3px; |
| 114 | + } |
| 115 | + `, |
| 116 | + config: configWithOptions(true), |
| 117 | + fix: true |
| 118 | + }) |
| 119 | + .then(data => { |
| 120 | + expect(data).not.toHaveErrored() |
| 121 | + expect(data).toHaveWarningsLength(0) |
| 122 | + expect(data.output).toEqual(dedent` |
| 123 | + .x { border-radius: $border-radius; } |
| 124 | + .y { |
| 125 | + border-top-left-radius: $border-radius; |
| 126 | + border-bottom-left-radius: $border-radius; |
| 127 | + } |
| 128 | + `) |
| 129 | + }) |
| 130 | + }) |
| 131 | + }) |
| 132 | +}) |
0 commit comments