Skip to content

Commit ec65b84

Browse files
committed
add tests for README examples and fix auto not being accepted from Top / Right / Bottom / Left utilities
1 parent fd097c6 commit ec65b84

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

src/config/default-config.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const SIZES_EXTENDED = ['3xl', '4xl', '5xl', '6xl', '7xl'] as const
55
const OVERSCROLL = ['auto', 'contain', 'none'] as const
66
const OVERFLOW = ['auto', 'hidden', 'visible', 'scroll'] as const
77
const LENGTH = [isLength] as const
8-
const MARGIN = ['auto', isLength] as const
8+
const LENGTH_WITH_AUTO = ['auto', isLength] as const
99
const INTEGER = [isInteger] as const
1010
const ANY = [isAny] as const
1111
const POSITIONS = [
@@ -172,45 +172,45 @@ export function getDefaultConfig() {
172172
* Right / Left
173173
* @see https://tailwindcss.com/docs/top-right-bottom-left
174174
*/
175-
[{ x: LENGTH }],
175+
[{ x: LENGTH_WITH_AUTO }],
176176
/**
177177
* Top / Bottom
178178
* @see https://tailwindcss.com/docs/top-right-bottom-left
179179
*/
180-
[{ y: LENGTH }],
180+
[{ y: LENGTH_WITH_AUTO }],
181181
/**
182182
* Top / Right / Bottom / Left
183183
* @see https://tailwindcss.com/docs/top-right-bottom-left
184184
*/
185-
LENGTH,
185+
LENGTH_WITH_AUTO,
186186
],
187187
top: [
188188
/**
189189
* Top
190190
* @see https://tailwindcss.com/docs/top-right-bottom-left
191191
*/
192-
LENGTH,
192+
LENGTH_WITH_AUTO,
193193
],
194194
right: [
195195
/**
196196
* Right
197197
* @see https://tailwindcss.com/docs/top-right-bottom-left
198198
*/
199-
LENGTH,
199+
LENGTH_WITH_AUTO,
200200
],
201201
bottom: [
202202
/**
203203
* Bottom
204204
* @see https://tailwindcss.com/docs/top-right-bottom-left
205205
*/
206-
LENGTH,
206+
LENGTH_WITH_AUTO,
207207
],
208208
left: [
209209
/**
210210
* Left
211211
* @see https://tailwindcss.com/docs/top-right-bottom-left
212212
*/
213-
LENGTH,
213+
LENGTH_WITH_AUTO,
214214
],
215215
z: [
216216
/**
@@ -449,49 +449,49 @@ export function getDefaultConfig() {
449449
* Margin
450450
* @see https://tailwindcss.com/docs/margin
451451
*/
452-
MARGIN,
452+
LENGTH_WITH_AUTO,
453453
],
454454
mx: [
455455
/**
456456
* Margin X
457457
* @see https://tailwindcss.com/docs/margin
458458
*/
459-
MARGIN,
459+
LENGTH_WITH_AUTO,
460460
],
461461
my: [
462462
/**
463463
* Margin Y
464464
* @see https://tailwindcss.com/docs/margin
465465
*/
466-
MARGIN,
466+
LENGTH_WITH_AUTO,
467467
],
468468
mt: [
469469
/**
470470
* Margin Top
471471
* @see https://tailwindcss.com/docs/margin
472472
*/
473-
MARGIN,
473+
LENGTH_WITH_AUTO,
474474
],
475475
mr: [
476476
/**
477477
* Margin Right
478478
* @see https://tailwindcss.com/docs/margin
479479
*/
480-
MARGIN,
480+
LENGTH_WITH_AUTO,
481481
],
482482
mb: [
483483
/**
484484
* Margin Bottom
485485
* @see https://tailwindcss.com/docs/margin
486486
*/
487-
MARGIN,
487+
LENGTH_WITH_AUTO,
488488
],
489489
ml: [
490490
/**
491491
* Margin Left
492492
* @see https://tailwindcss.com/docs/margin
493493
*/
494-
MARGIN,
494+
LENGTH_WITH_AUTO,
495495
],
496496
space: [
497497
/**

tests/readme-examples.test.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { twMerge } from '../src'
2+
3+
test('readme examples', () => {
4+
// # tailwind-merge
5+
expect(twMerge('px-2 py-1 bg-red hover:bg-dark-red', 'p-3 bg-[#B91C1C]')).toBe(
6+
'hover:bg-dark-red p-3 bg-[#B91C1C]'
7+
)
8+
9+
// ## What is it for
10+
expect(twMerge('px-2 py-1', 'p-3')).toBe('p-3')
11+
12+
// ## Features
13+
// ### Last conflicting class wins
14+
expect(twMerge('p-5 p-2 p-4')).toBe('p-4')
15+
16+
// ### Resolves non-trivial conflicts
17+
expect(twMerge('inset-x-px -inset-1')).toBe('-inset-1')
18+
expect(twMerge('bottom-auto inset-y-6')).toBe('inset-y-6')
19+
expect(twMerge('inline block')).toBe('block')
20+
21+
// ### Supports prefixes and stacked prefixes
22+
expect(twMerge('p-2 hover:p-4')).toBe('p-2 hover:p-4')
23+
expect(twMerge('hover:p-2 hover:p-4')).toBe('hover:p-4')
24+
expect(twMerge('hover:focus:p-2 focus:hover:p-4')).toBe('focus:hover:p-4')
25+
26+
// ### Supports custom values
27+
expect(twMerge('bg-black bg-[color:var(--mystery-var)]')).toBe('bg-[color:var(--mystery-var)]')
28+
expect(twMerge('grid-cols-[1fr,auto] grid-cols-2')).toBe('grid-cols-2')
29+
30+
// ### Supports important modifier
31+
expect(twMerge('!p-3 !p-4 p-5')).toBe('!p-4 p-5')
32+
expect(twMerge('!right-2 !-inset-x-1')).toBe('!-inset-x-1')
33+
34+
// ### Preserves non-Tailwind classes
35+
expect(twMerge('p-5 p-2 my-non-tailwind-class p-4')).toBe('my-non-tailwind-class p-4')
36+
37+
// ### Supports custom colors out of the box
38+
expect(twMerge('text-red text-secret-sauce')).toBe('text-secret-sauce')
39+
})

0 commit comments

Comments
 (0)