|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import { describe, test, expect } from "vitest"; |
| 4 | +import stylelint from "stylelint"; |
| 5 | + |
| 6 | +import { configBasedir } from "./common.js"; |
| 7 | + |
| 8 | +// This is for prettier format: https://github.com/prettier/prettier/issues/2330 |
| 9 | +// String.raw is an identity function in this context |
| 10 | +const css = String.raw; |
| 11 | + |
| 12 | +function runPlugin(code) { |
| 13 | + return stylelint.lint({ |
| 14 | + code, |
| 15 | + configBasedir, |
| 16 | + config: { |
| 17 | + plugins: ["../z-index-value-constraint.js"], |
| 18 | + rules: { |
| 19 | + "@cloudscape-design/z-index-value-constraint": [true], |
| 20 | + }, |
| 21 | + }, |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +describe("z-index value contstraint rule", () => { |
| 26 | + test("allows non integer z-index values", async () => { |
| 27 | + const result = await runPlugin(css` |
| 28 | + .styled-circle-motion { |
| 29 | + @include styles.with-motion { |
| 30 | + z-index: some.$variable; |
| 31 | + } |
| 32 | + } |
| 33 | + `); |
| 34 | + |
| 35 | + expect(result.errored).toBe(false); |
| 36 | + }); |
| 37 | + |
| 38 | + test.each([0, 1000])("allows non negative z-index values: %s", async zIndexValue => { |
| 39 | + const result = await runPlugin(css` |
| 40 | + .styled-circle-motion { |
| 41 | + @include styles.with-motion { |
| 42 | + z-index: ${zIndexValue}; |
| 43 | + } |
| 44 | + } |
| 45 | + `); |
| 46 | + |
| 47 | + expect(result.errored).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + test("does not allow negative z-index values", async () => { |
| 51 | + const result = await runPlugin(css` |
| 52 | + .styled-circle-motion { |
| 53 | + @include styles.with-motion { |
| 54 | + z-index: -10; |
| 55 | + } |
| 56 | + } |
| 57 | + `); |
| 58 | + |
| 59 | + expect(result.errored).toBe(true); |
| 60 | + expect(result.results[0].warnings[0].text).toBe(`Avoid using negative z-index values: -10. |
| 61 | + This can cause the element to disappear behind its container's background color. (@cloudscape-design/z-index-value-constraint)`); |
| 62 | + }); |
| 63 | +}); |
0 commit comments