|
| 1 | +import { ac, memoizedAc, getCache } from '../ac'; |
| 2 | + |
| 3 | +describe('ac', () => { |
| 4 | + const isEnabled: boolean = (() => false)(); |
| 5 | + |
| 6 | + it.each([ |
| 7 | + ['should handle empty array', [], undefined], |
| 8 | + ['should handle array with undefined', [undefined], undefined], |
| 9 | + ['should join single classes together', ['foo', 'bar'], 'foo bar'], |
| 10 | + ['should join multi classes together', ['foo baz', 'bar'], 'foo baz bar'], |
| 11 | + ['should remove undefined', ['foo', 'bar', undefined], 'foo bar'], |
| 12 | + [ |
| 13 | + 'should ensure the last atomic declaration of a single group wins', |
| 14 | + ['_aaaabbbb', '_aaaacccc'], |
| 15 | + '_aaaacccc', |
| 16 | + ], |
| 17 | + [ |
| 18 | + 'should ensure the last atomic declaration of a single group with short class name wins', |
| 19 | + ['_aaaabbbb', '_aaaacccc', '_aaaa_a'], |
| 20 | + 'a', |
| 21 | + ], |
| 22 | + [ |
| 23 | + 'should ensure the last atomic declaration of many single groups wins', |
| 24 | + ['_aaaabbbb', '_aaaacccc', '_aaaadddd', '_aaaaeeee'], |
| 25 | + '_aaaaeeee', |
| 26 | + ], |
| 27 | + [ |
| 28 | + 'should ensure the last atomic declaration of many single groups with short class name wins', |
| 29 | + ['_aaaabbbb', '_aaaacccc', '_aaaa_a', '_aaaa_b'], |
| 30 | + 'b', |
| 31 | + ], |
| 32 | + [ |
| 33 | + 'should ensure the last atomic declaration of a multi group wins', |
| 34 | + ['_aaaabbbb _aaaacccc'], |
| 35 | + '_aaaacccc', |
| 36 | + ], |
| 37 | + [ |
| 38 | + 'should ensure the last atomic declaration of a multi group with short class name wins', |
| 39 | + ['_aaaa_e', '_aaaabbbb _aaaacccc'], |
| 40 | + '_aaaacccc', |
| 41 | + ], |
| 42 | + [ |
| 43 | + 'should ensure the last atomic declaration of many multi groups wins', |
| 44 | + ['_aaaabbbb _aaaacccc _aaaadddd _aaaaeeee'], |
| 45 | + '_aaaaeeee', |
| 46 | + ], |
| 47 | + [ |
| 48 | + 'should ensure the last atomic declaration of many multi groups with short class name wins', |
| 49 | + ['_aaaabbbb', '_aaaa_a', '_bbbb_b', '_ddddcccc'], |
| 50 | + 'a b _ddddcccc', |
| 51 | + ], |
| 52 | + [ |
| 53 | + 'should not remove any atomic declarations if there are no duplicate groups', |
| 54 | + ['_aaaabbbb', '_bbbbcccc'], |
| 55 | + '_aaaabbbb _bbbbcccc', |
| 56 | + ], |
| 57 | + [ |
| 58 | + 'should not remove any atomic declarations if there are short class name and no duplicate groups', |
| 59 | + ['_eeee_e', '_aaaabbbb', '_bbbbcccc'], |
| 60 | + 'e _aaaabbbb _bbbbcccc', |
| 61 | + ], |
| 62 | + ['should not apply conditional class', [isEnabled && 'foo', 'bar'], 'bar'], |
| 63 | + [ |
| 64 | + 'should ignore non atomic declarations', |
| 65 | + ['hello_there', 'hello_world'], |
| 66 | + 'hello_there hello_world', |
| 67 | + ], |
| 68 | + [ |
| 69 | + 'should ignore non atomic declarations when atomic declarations exist', |
| 70 | + ['hello_there', 'hello_world', '_aaaabbbb'], |
| 71 | + 'hello_there hello_world _aaaabbbb', |
| 72 | + ], |
| 73 | + [ |
| 74 | + 'should ignore non atomic declarations when atomic declarations with short class name exist', |
| 75 | + ['hello_there', 'hello_world', '_aaaa_a'], |
| 76 | + 'hello_there hello_world a', |
| 77 | + ], |
| 78 | + ])('%s', (_, params, result) => { |
| 79 | + expect(result).toEqual(ac(params)?.toString()); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should ensure the last atomic declaration wins if calling ax multiple times with short class names', () => { |
| 83 | + expect(ac([ac(['_aaaa_b']), '_aaaa_c'])?.toString()).toEqual('c'); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +describe('memoizedAc', () => { |
| 88 | + it('should cache correctly', () => { |
| 89 | + memoizedAc([memoizedAc(['_aaaa_b', '_aaaabbbb', 'hello_world']), '_bbbb_d', '_aaaa_e']); |
| 90 | + |
| 91 | + expect(getCache()).toMatchInlineSnapshot(` |
| 92 | + Map { |
| 93 | + "_aaaa_b _aaaabbbb hello_world" => AtomicGroups { |
| 94 | + "values": Map { |
| 95 | + "_aaaa" => "_aaaabbbb", |
| 96 | + "hello_world" => "hello_world", |
| 97 | + }, |
| 98 | + }, |
| 99 | + "_aaaabbbb hello_world _bbbb_d _aaaa_e" => AtomicGroups { |
| 100 | + "values": Map { |
| 101 | + "_aaaa" => "e", |
| 102 | + "hello_world" => "hello_world", |
| 103 | + "_bbbb" => "d", |
| 104 | + }, |
| 105 | + }, |
| 106 | + } |
| 107 | + `); |
| 108 | + }); |
| 109 | + it('should not create a new ref', () => { |
| 110 | + expect(memoizedAc(['a'])).toBe(memoizedAc(['a'])); |
| 111 | + }); |
| 112 | +}); |
0 commit comments