Skip to content

Commit

Permalink
chore(test): add expect.extend
Browse files Browse the repository at this point in the history
  • Loading branch information
sonofmagic committed Jan 6, 2025
1 parent 3d4ac46 commit 146b289
Show file tree
Hide file tree
Showing 22 changed files with 871 additions and 482 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"@commitlint/prompt-cli": "^19.7.0",
"@commitlint/types": "^19.5.0",
"@eslint/config-inspector": "^0.7.0",
"@icebreakers/eslint-config": "^0.7.8",
"@icebreakers/eslint-config": "^0.7.9",
"@icebreakers/monorepo": "^0.6.21",
"@icebreakers/readme": "0.1.0",
"@icebreakers/stylelint-config": "^0.1.3",
Expand Down Expand Up @@ -210,7 +210,7 @@
"prettier": "^3.4.2",
"promisify-loader-runner": "^1.0.1",
"rimraf": "^6.0.1",
"rollup": "^4.29.2",
"rollup": "^4.30.0",
"rollup-plugin-visualizer": "^5.13.1",
"sass": "^1.83.1",
"sass-embedded": "^1.83.1",
Expand Down
4 changes: 3 additions & 1 deletion packages/tailwind-merge-weapp-plugin/src/default-config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Config, DefaultClassGroupIds, DefaultThemeGroupIds } from './types'
// import type { Config, DefaultClassGroupIds, DefaultThemeGroupIds } from 'tailwind-merge'
// import type { ThemeGetter } from 'tailwind-merge'
import { fromTheme } from './from-theme'
Expand Down Expand Up @@ -77,6 +78,7 @@ export function getDefaultConfig() {

return {
// cacheSize: 500,
// separator: SimpleMappingChars2String[':'],
// separator: ':',
theme: {
colors: [isAny],
Expand Down Expand Up @@ -1859,5 +1861,5 @@ export function getDefaultConfig() {
conflictingClassGroupModifiers: {
'font-size': ['leading'],
},
}// as const satisfies Omit<Config<DefaultClassGroupIds, DefaultThemeGroupIds>, 'cacheSize' | 'prefix' | 'separator'>
} as const satisfies Config<DefaultClassGroupIds, DefaultThemeGroupIds>// Omit<Config<DefaultClassGroupIds, DefaultThemeGroupIds>, 'cacheSize' | 'prefix' | 'separator'>
}
7 changes: 6 additions & 1 deletion packages/tailwind-merge-weapp-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
// https://github.com/dcastil/tailwind-merge/blob/main/src/lib/default-config.ts

import type { Config } from 'tailwind-merge'
import { SimpleMappingChars2String } from '@weapp-core/escape'
import { mergeConfigs } from 'tailwind-merge'
import { getDefaultConfig } from './default-config'

// https://github.com/vltansky/tailwind-merge-rtl-plugin
// https://github.com/kaelansmith/tailwind-extended-shadows-merge
export function withWeapp(config: Config<string, string>): Config<string, string> {
return mergeConfigs(config, {
separator: SimpleMappingChars2String[':'],
extend: getDefaultConfig(),
})
}

export {
getDefaultConfig,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { expect, it } from 'vitest'

import { twMerge } from './utils'
import { replaceJs, twMergeReplaceJs as twMerge } from './utils'

expect.extend({
toBe: (received, expected) => {
const target = replaceJs(expected)
if (received !== target) {
return {
message: () => `expected ${received} to be ${target}`,
pass: false,
}
}
else {
return {
message: () => ``,
pass: true,
}
}
},
})

it('handles arbitrary property conflicts correctly', () => {
expect(twMerge('[paint-order:markers] [paint-order:normal]')).toBe('[paint-order:normal]')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { expect, it } from 'vitest'

import { twMerge } from './utils'
import { replaceJs, twMergeReplaceJs as twMerge } from './utils'

expect.extend({
toBe: (received, expected) => {
const target = replaceJs(expected)
if (received !== target) {
return {
message: () => `expected ${received} to be ${target}`,
pass: false,
}
}
else {
return {
message: () => ``,
pass: true,
}
}
},
})

it('handles simple conflicts with arbitrary values correctly', () => {
expect(twMerge('m-[2px] m-[10px]')).toBe('m-[10px]')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { expect, it } from 'vitest'

import { twMerge } from './utils'
import { replaceJs, twMergeReplaceJs as twMerge } from './utils'

expect.extend({
toBe: (received, expected) => {
const target = replaceJs(expected)
if (received !== target) {
return {
message: () => `expected ${received} to be ${target}`,
pass: false,
}
}
else {
return {
message: () => ``,
pass: true,
}
}
},
})

it('basic arbitrary variants', () => {
expect(twMerge('[&>*]:underline [&>*]:line-through')).toBe('[&>*]:line-through')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { expect, it } from 'vitest'

import { twMerge } from './utils'
import { replaceJs, twMergeReplaceJs as twMerge } from './utils'

expect.extend({
toBe: (received, expected) => {
const target = replaceJs(expected)
if (received !== target) {
return {
message: () => `expected ${received} to be ${target}`,
pass: false,
}
}
else {
return {
message: () => ``,
pass: true,
}
}
},
})

it('merges classes from same group correctly', () => {
expect(twMerge('overflow-x-auto overflow-x-hidden')).toBe('overflow-x-hidden')
Expand Down
20 changes: 19 additions & 1 deletion packages/tailwind-merge-weapp-plugin/test/colors.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { expect, it } from 'vitest'

import { twMerge } from './utils'
import { replaceJs, twMergeReplaceJs as twMerge } from './utils'

expect.extend({
toBe: (received, expected) => {
const target = replaceJs(expected)
if (received !== target) {
return {
message: () => `expected ${received} to be ${target}`,
pass: false,
}
}
else {
return {
message: () => ``,
pass: true,
}
}
},
})

it('handles color conflicts properly', () => {
expect(twMerge('bg-grey-5 bg-hotpink')).toBe('bg-hotpink')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { expect, it } from 'vitest'

import { twMerge } from './utils'
import { replaceJs, twMergeReplaceJs as twMerge } from './utils'

expect.extend({
toBe: (received, expected) => {
const target = replaceJs(expected)
if (received !== target) {
return {
message: () => `expected ${received} to be ${target}`,
pass: false,
}
}
else {
return {
message: () => ``,
pass: true,
}
}
},
})

it('handles conflicts across class groups correctly', () => {
expect(twMerge('inset-1 inset-x-1')).toBe('inset-1 inset-x-1')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { expect, it } from 'vitest'

import { twMerge } from './utils'
import { replaceJs, twMergeReplaceJs as twMerge } from './utils'

expect.extend({
toBe: (received, expected) => {
const target = replaceJs(expected)
if (received !== target) {
return {
message: () => `expected ${received} to be ${target}`,
pass: false,
}
}
else {
return {
message: () => ``,
pass: true,
}
}
},
})

it('merges content utilities correctly', () => {
expect(twMerge('content-[\'hello\'] content-[attr(data-content)]')).toBe(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { expect, it } from 'vitest'

import { twMerge } from './utils'
import { replaceJs, twMergeReplaceJs as twMerge } from './utils'

expect.extend({
toBe: (received, expected) => {
const target = replaceJs(expected)
if (received !== target) {
return {
message: () => `expected ${received} to be ${target}`,
pass: false,
}
}
else {
return {
message: () => ``,
pass: true,
}
}
},
})

it('merges tailwind classes with important modifier correctly', () => {
expect(twMerge('!font-medium !font-bold')).toBe('!font-bold')
Expand Down
20 changes: 19 additions & 1 deletion packages/tailwind-merge-weapp-plugin/test/modifiers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { expect, it } from 'vitest'

import { createTailwindMerge, twMerge } from './utils'
import { createTailwindMerge, replaceJs, twMergeReplaceJs as twMerge } from './utils'

expect.extend({
toBe: (received, expected) => {
const target = replaceJs(expected)
if (received !== target) {
return {
message: () => `expected ${received} to be ${target}`,
pass: false,
}
}
else {
return {
message: () => ``,
pass: true,
}
}
},
})

it('conflicts across prefix modifiers', () => {
expect(twMerge('hover:block hover:inline')).toBe('hover:inline')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { expect, it } from 'vitest'

import { twMerge } from './utils'
import { replaceJs, twMergeReplaceJs as twMerge } from './utils'

expect.extend({
toBe: (received, expected) => {
const target = replaceJs(expected)
if (received !== target) {
return {
message: () => `expected ${received} to be ${target}`,
pass: false,
}
}
else {
return {
message: () => ``,
pass: true,
}
}
},
})

it('handles negative value conflicts correctly', () => {
expect(twMerge('-m-2 -m-5')).toBe('-m-5')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { expect, it } from 'vitest'

import { twMerge } from './utils'
import { replaceJs, twMergeReplaceJs as twMerge } from './utils'

expect.extend({
toBe: (received, expected) => {
const target = replaceJs(expected)
if (received !== target) {
return {
message: () => `expected ${received} to be ${target}`,
pass: false,
}
}
else {
return {
message: () => ``,
pass: true,
}
}
},
})

it('merges non-conflicting classes correctly', () => {
expect(twMerge('border-t border-white/10')).toBe('border-t border-white/10')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { expect, it } from 'vitest'

import { twMerge } from './utils'
import { replaceJs, twMergeReplaceJs as twMerge } from './utils'

expect.extend({
toBe: (received, expected) => {
const target = replaceJs(expected)
if (received !== target) {
return {
message: () => `expected ${received} to be ${target}`,
pass: false,
}
}
else {
return {
message: () => ``,
pass: true,
}
}
},
})

it('does not alter non-tailwind classes', () => {
expect(twMerge('non-tailwind-class inline block')).toBe('non-tailwind-class block')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { expect, it } from 'vitest'

import { twMerge } from './utils'
import { replaceJs, twMergeReplaceJs as twMerge } from './utils'

expect.extend({
toBe: (received, expected) => {
const target = replaceJs(expected)
if (received !== target) {
return {
message: () => `expected ${received} to be ${target}`,
pass: false,
}
}
else {
return {
message: () => ``,
pass: true,
}
}
},
})

it('merges classes with per-side border colors correctly', () => {
expect(twMerge('border-t-some-blue border-t-other-blue')).toBe('border-t-other-blue')
Expand Down
Loading

0 comments on commit 146b289

Please sign in to comment.