Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add tests to check the theme config #74

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions packages/tailwindcss-animations/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@
*/
export const matchUtilitiesRegex = new RegExp(/^(animation(?=Delay|Duration|Steps|IterationCount|FillMode|Range))/)

/**
* Match utilities that are generated by addUtilities function provided by TailwindCSS.
* @example
*
* // Expected: true
* addUtilitiesRegex.test("animation")
*
* // Expected: false
* addUtilitiesRegex.test("animation-delay-100")
*/
export const addUtilitiesRegex = new RegExp(/^(animation|keyframes|animationCubicBezier)/)

/**
* Converts a camelCase string to a slash-case string.
*
Expand Down
36 changes: 26 additions & 10 deletions packages/tailwindcss-animations/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, test, expect } from "vitest"
import { extractClasses } from "@halvaradop/tailwindcss-core/utils"
import plugin from "../src/index.js"
import { toSlashCase } from "../src/utils.js"
import { plugin } from "../src/index.js"
import { theme } from "../src/theme.js"

export const generateClasses = extractClasses(plugin)

Expand All @@ -11,9 +11,15 @@ describe("Plugin", () => {
const html = `<div class="animation-delay-100"></div>`
const css = await generateClasses(html)
expect(css).toMatch(".animation-delay-100{animation-delay:100ms}")
expect(css).not.toMatch(".animation-delay-200{animation-delay:200ms}")
})
})

test("Loaded keyframes utility classes", async () => {
expect(plugin.config?.theme?.keyframes).toEqual(theme.keyframes)
expect(plugin.config?.theme?.keyframes).not.toEqual({})
})

describe("Conflict prevention", () => {
test.concurrent("should not create conflicting classes", async () => {
const html = `<div class="w-full px-2 flex items-center"></div>`
Expand Down Expand Up @@ -56,17 +62,27 @@ describe("Animate delay utility classes", () => {
})
})

describe("toSlashCase", () => {
describe("Keyframes utility classes", () => {
const testCases = [
{ input: "animation", expected: "animation" },
{ input: "animationDelay", expected: "animation-delay" },
{ input: "animationDuration", expected: "animation-duration" },
{ input: "animationIterationCount", expected: "animation-iteration-count" },
{
className: "animate-fade-in",
expected: "@keyframes fade-in{0%{opacity:0}100%{opacity:1}}",
},
{
className: "animate-fade-out",
expected: "@keyframes fade-out{0%{opacity:1}100%{opacity:0}}",
},
{
className: "animate-rotate-90",
expected: "@keyframes rotate-90{0%{transform:rotate(0deg)}100%{transform:rotate(90deg)}}",
},
]

testCases.forEach(({ input, expected }) => {
test(`converts ${input} to ${expected}`, () => {
expect(toSlashCase(input)).toBe(expected)
testCases.forEach(({ className, expected }) => {
test.concurrent(`keyframes with class ${className}`, async () => {
const html = `<div class="${className}"></div>`
const css = await generateClasses(html)
expect(css).toMatch(expected)
})
})
})
26 changes: 26 additions & 0 deletions packages/tailwindcss-animations/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, test, expect } from "vitest"
import { toSlashCase, matchUtilitiesRegex } from "../src/utils.js"

describe("toSlashCase", () => {
const testCases = [
{ input: "animation", expected: "animation" },
{ input: "animationDelay", expected: "animation-delay" },
{ input: "animationDuration", expected: "animation-duration" },
{ input: "animationIterationCount", expected: "animation-iteration-count" },
]

testCases.forEach(({ input, expected }) => {
test(`converts ${input} to ${expected}`, () => {
expect(toSlashCase(input)).toBe(expected)
})
})
})

describe("matchUtilitiesRegex", () => {
test("matches animation utilities", () => {
expect(matchUtilitiesRegex.test("animationDelay")).toBe(true)
expect(matchUtilitiesRegex.test("animationDuration")).toBe(true)
expect(matchUtilitiesRegex.test("animation")).toBe(false)
expect(matchUtilitiesRegex.test("keyframes")).toBe(false)
})
})