|
| 1 | +import { resolve } from "path"; |
| 2 | +import fs from "fs"; |
| 3 | +import preProcessContent, { JS_BLOCK } from "../../pre-processors"; |
| 4 | +import { parseFiles } from "../../parser"; |
| 5 | +import { CACHE, Config, CSSVarRecord, DEFAULT_CONFIG } from "../../constants"; |
| 6 | + |
| 7 | +jest.mock("../../constants", () => { |
| 8 | + const CONSTANTS = jest.requireActual("../../constants"); |
| 9 | + const activeRootPath = "foo"; |
| 10 | + return { |
| 11 | + __esModule: true, |
| 12 | + ...CONSTANTS, |
| 13 | + CACHE: { |
| 14 | + ...CONSTANTS.CACHE, |
| 15 | + activeRootPath, |
| 16 | + config: { [activeRootPath]: CONSTANTS.DEFAULT_CONFIG }, |
| 17 | + }, |
| 18 | + }; |
| 19 | +}); |
| 20 | + |
| 21 | +type ConfigRecord = { [rootPath: string]: Config }; |
| 22 | + |
| 23 | +const MODIFIED_DATE = new Date("2021-04-12T08:58:58.676Z"); |
| 24 | +const JSX_FILE_PATH = resolve(__dirname, "..", "fixtures", "edge-cases.jsx"); |
| 25 | +const MOCK_CONFIG: ConfigRecord = { |
| 26 | + [CACHE.activeRootPath]: { |
| 27 | + ...DEFAULT_CONFIG, |
| 28 | + files: [JSX_FILE_PATH], |
| 29 | + }, |
| 30 | +}; |
| 31 | + |
| 32 | +const RESULTS = [ |
| 33 | + { |
| 34 | + type: "css", |
| 35 | + property: "--h1", |
| 36 | + value: "44px", |
| 37 | + theme: "", |
| 38 | + }, |
| 39 | + { |
| 40 | + type: "css", |
| 41 | + property: "--h2", |
| 42 | + value: "32px", |
| 43 | + theme: "", |
| 44 | + }, |
| 45 | + { |
| 46 | + type: "css", |
| 47 | + property: "--h3", |
| 48 | + value: "24px", |
| 49 | + theme: "", |
| 50 | + }, |
| 51 | + { |
| 52 | + type: "css", |
| 53 | + property: "--color-red", |
| 54 | + value: "red", |
| 55 | + theme: "", |
| 56 | + color: "rgb(255, 0, 0)", |
| 57 | + }, |
| 58 | + { |
| 59 | + type: "css", |
| 60 | + property: "--color-green", |
| 61 | + value: "green", |
| 62 | + theme: "", |
| 63 | + color: "rgb(0, 128, 0)", |
| 64 | + }, |
| 65 | + { |
| 66 | + type: "css", |
| 67 | + property: "--color-blue", |
| 68 | + value: "blue", |
| 69 | + theme: "", |
| 70 | + color: "rgb(0, 0, 255)", |
| 71 | + }, |
| 72 | + { |
| 73 | + type: "css", |
| 74 | + property: "--base-var-1", |
| 75 | + value: "#333", |
| 76 | + theme: "", |
| 77 | + color: "rgb(51, 51, 51)", |
| 78 | + }, |
| 79 | + { |
| 80 | + type: "css", |
| 81 | + property: "--child-var-1", |
| 82 | + value: "#222", |
| 83 | + theme: "", |
| 84 | + color: "rgb(34, 34, 34)", |
| 85 | + }, |
| 86 | + |
| 87 | + // This should belong in WRONG_RESULTS, but for now |
| 88 | + // it's fine |
| 89 | + { |
| 90 | + type: "css", |
| 91 | + property: "--random", |
| 92 | + value: "error", |
| 93 | + theme: "", |
| 94 | + }, |
| 95 | +]; |
| 96 | + |
| 97 | +const WRONG_RESULTS = [ |
| 98 | + { |
| 99 | + type: "css", |
| 100 | + property: "--", |
| 101 | + value: JS_BLOCK, |
| 102 | + }, |
| 103 | + { |
| 104 | + type: "css", |
| 105 | + property: "--fuzz", |
| 106 | + value: JS_BLOCK, |
| 107 | + }, |
| 108 | + { |
| 109 | + type: "css", |
| 110 | + property: "--flex", |
| 111 | + }, |
| 112 | + { |
| 113 | + type: "css", |
| 114 | + property: "--baz", |
| 115 | + }, |
| 116 | +]; |
| 117 | + |
| 118 | +let globalVarRecord: CSSVarRecord; |
| 119 | +beforeAll(() => { |
| 120 | + CACHE.filesToWatch[CACHE.activeRootPath] = new Set(); |
| 121 | + CACHE.fileMetas = {}; |
| 122 | + CACHE.fileMetas[JSX_FILE_PATH] = { |
| 123 | + path: JSX_FILE_PATH, |
| 124 | + lastModified: +MODIFIED_DATE, |
| 125 | + }; |
| 126 | + return parseFiles(MOCK_CONFIG).then(([varRecord, _]) => { |
| 127 | + globalVarRecord = varRecord; |
| 128 | + return varRecord; |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +test("should have proper variables present", () => { |
| 133 | + for (const result of RESULTS) { |
| 134 | + expect(globalVarRecord[JSX_FILE_PATH]).toContainEqual(expect.objectContaining(result)); |
| 135 | + } |
| 136 | +}); |
| 137 | + |
| 138 | +test("should not have improper variables", () => { |
| 139 | + for (const wrong of WRONG_RESULTS) { |
| 140 | + expect(globalVarRecord[JSX_FILE_PATH]).not.toContainEqual(expect.objectContaining(wrong)); |
| 141 | + } |
| 142 | +}); |
0 commit comments