Skip to content

Commit

Permalink
add tests for replaceFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielAmenou-R2net committed Jul 4, 2023
1 parent 83104b4 commit 3216386
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions test/functions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import crypto from "node:crypto"
import {replaceFormat} from "../src/functions"

describe("replaceFormat function", () => {
it("replaces [local] and [hash:base64:6] correctly", () => {
const formatString = "[local]_[hash:base64:6]"
const fileName = "testFile"
const cssContent = ".myClass { background-color: #000; }"
const hash = crypto.createHash("md5").update(cssContent).digest("base64").slice(0, 6)

const result = replaceFormat(formatString, fileName, cssContent)

expect(result).toEqual(`${fileName}_${hash}`)
})

it("replaces [local] and [hash:base64] correctly", () => {
const formatString = "[local]_[hash:base64:3]"
const fileName = "testFile"
const cssContent = ".myClass { background-color: #000; }"
const hash = crypto.createHash("md5").update(cssContent).digest("base64").slice(0, 3)

const result = replaceFormat(formatString, fileName, cssContent)

expect(result).toEqual(`${fileName}_${hash}`)
})

it("replaces [local] and [hash:6] correctly", () => {
const formatString = "[local]_[hash:6]"
const fileName = "testFile"
const cssContent = ".myClass { background-color: #000; }"
const hash = crypto.createHash("md5").update(cssContent).digest("hex").slice(0, 6)

const result = replaceFormat(formatString, fileName, cssContent)

expect(result).toEqual(`${fileName}_${hash}`)
})

it("replaces [local] correctly", () => {
const formatString = "[local]"
const fileName = "testFile"
const cssContent = ".myClass { background-color: #000; }"

const result = replaceFormat(formatString, fileName, cssContent)

expect(result).toEqual(fileName)
})
})

0 comments on commit 3216386

Please sign in to comment.