-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(figma-utils): add unit tests (#101)
closes #99 ## Description Add unit tests for the `@sit-onyx/figma-utils` package which tests all main use cases.
- Loading branch information
1 parent
e8f6f01
commit eebf509
Showing
10 changed files
with
435 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@sit-onyx/figma-utils": patch | ||
--- | ||
|
||
fix(parse): remove mode name if its the default Figma mode name "Mode 1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/figma-utils/src/commands/import-variables.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import fs from "node:fs"; | ||
import { beforeEach, describe, expect, test, vi } from "vitest"; | ||
import * as functions from "../index.js"; | ||
import { ImportCommandOptions, importCommandAction } from "./import-variables.js"; | ||
|
||
vi.mock("node:fs"); | ||
|
||
vi.mock("../index.js"); | ||
|
||
describe("import-variables.ts", () => { | ||
const mockOptions = { | ||
fileKey: "test-file-key", | ||
filename: "test-file-name", | ||
format: "CSS", | ||
token: "test-token", | ||
selector: ":root", | ||
} satisfies ImportCommandOptions; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
vi.spyOn(console, "log").mockImplementation(() => ({})); | ||
vi.spyOn(process, "cwd").mockReturnValue("test-cwd"); | ||
}); | ||
|
||
test("should throw error for unknown formats", () => { | ||
const promise = () => importCommandAction({ ...mockOptions, format: "does-not-exist" }); | ||
expect(promise).rejects.toThrowError("Unknown format: does-not-exist. Supported: CSS, SCSS"); | ||
}); | ||
|
||
test("should throw error for unknown modes", () => { | ||
vi.spyOn(functions, "parseFigmaVariables").mockReturnValue([ | ||
{ modeName: "test-mode-1", variables: {} }, | ||
]); | ||
|
||
const promise = () => | ||
importCommandAction({ | ||
...mockOptions, | ||
modes: ["test-mode-1", "does-not-exist"], | ||
}); | ||
expect(promise).rejects.toThrowError( | ||
'Mode "does-not-exist" not found. Available modes: "test-mode-1"', | ||
); | ||
}); | ||
|
||
test("should generate variables", async () => { | ||
vi.spyOn(functions, "parseFigmaVariables").mockReturnValue([ | ||
{ modeName: "test-mode-1", variables: {} }, | ||
{ modeName: "test-mode-2", variables: {} }, | ||
{ modeName: "test-mode-3", variables: {} }, | ||
]); | ||
|
||
vi.spyOn(functions, "generateAsCSS").mockReturnValue("mock-css-file-content"); | ||
|
||
await importCommandAction({ ...mockOptions, modes: ["test-mode-1", "test-mode-2"] }); | ||
|
||
expect(functions.fetchFigmaVariables).toHaveBeenCalledOnce(); | ||
expect(functions.parseFigmaVariables).toHaveBeenCalledOnce(); | ||
expect(functions.generateAsCSS).toHaveBeenCalledTimes(2); | ||
expect(fs.writeFileSync).toHaveBeenCalledTimes(2); | ||
|
||
expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
"test-cwd/test-file-name-test-mode-1.css", | ||
"mock-css-file-content", | ||
); | ||
|
||
expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
"test-cwd/test-file-name-test-mode-2.css", | ||
"mock-css-file-content", | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { ParsedVariable } from "src/index.js"; | ||
import { beforeEach, describe, expect, test, vi } from "vitest"; | ||
import { generateAsCSS, generateAsSCSS } from "./generate.js"; | ||
|
||
describe("generate.ts", () => { | ||
const mockData = { | ||
modeName: "test-mode-1", | ||
variables: { | ||
"test-1": "#ffffff", | ||
"test-2": "1rem", | ||
"test-3": "{test-2}", | ||
}, | ||
} satisfies ParsedVariable; | ||
|
||
beforeEach(() => { | ||
vi.setSystemTime(new Date(2024, 0, 7, 13, 42)); | ||
}); | ||
|
||
test("should generate as CSS", () => { | ||
const fileContent = generateAsCSS(mockData); | ||
|
||
expect(fileContent).toBe(`/** | ||
* Do not edit directly. | ||
* This file contains the specific variables for the "test-mode-1" theme. | ||
* Imported from Figma API on Sun, 07 Jan 2024 12:42:00 GMT | ||
*/ | ||
:root { | ||
--test-1: #ffffff; | ||
--test-2: 1rem; | ||
--test-3: var(--test-2); | ||
} | ||
`); | ||
}); | ||
|
||
test("should generate as CSS with custom selector", () => { | ||
const fileContent = generateAsCSS(mockData, "html"); | ||
|
||
expect(fileContent).toBe(`/** | ||
* Do not edit directly. | ||
* This file contains the specific variables for the "test-mode-1" theme. | ||
* Imported from Figma API on Sun, 07 Jan 2024 12:42:00 GMT | ||
*/ | ||
html.test-mode-1 { | ||
--test-1: #ffffff; | ||
--test-2: 1rem; | ||
--test-3: var(--test-2); | ||
} | ||
`); | ||
}); | ||
|
||
test("should generate as SCSS", () => { | ||
const fileContent = generateAsSCSS(mockData); | ||
|
||
expect(fileContent).toBe(`/** | ||
* Do not edit directly. | ||
* This file contains the specific variables for the "test-mode-1" theme. | ||
* Imported from Figma API on Sun, 07 Jan 2024 12:42:00 GMT | ||
*/ | ||
$test-1: #ffffff; | ||
$test-2: 1rem; | ||
$test-3: $test-2; | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.