|
1 | | -import { describe, expect, test } from 'bun:test'; |
| 1 | +/// <reference lib="dom" /> |
| 2 | +import { afterEach, describe, expect, test } from 'bun:test'; |
| 3 | +import { Window } from 'happy-dom'; |
2 | 4 | import * as preset from './preset'; |
3 | 5 |
|
| 6 | +let win: Window | undefined; |
| 7 | + |
| 8 | +function mount(): void { |
| 9 | + let w: Window = win = new Window({ url: 'https://example.com/' }); |
| 10 | + w.document.write('<!doctype html><html><head></head><body></body></html>'); |
| 11 | + Reflect.set(globalThis, 'document', w.document); |
| 12 | +} |
| 13 | + |
| 14 | +function tags(): string[] { |
| 15 | + return [ ...document.head.querySelectorAll('style[data-bongo-preset]') ].map(el => el.outerHTML); |
| 16 | +} |
| 17 | + |
4 | 18 | describe('preset.resolve()', () => { |
5 | 19 | test('true resolves to "fade"', () => { |
6 | 20 | expect(preset.resolve(true)).toBe('fade'); |
@@ -30,3 +44,44 @@ describe('preset.resolve()', () => { |
30 | 44 | expect(preset.resolve('my-custom-preset')).toBe('my-custom-preset'); |
31 | 45 | }); |
32 | 46 | }); |
| 47 | + |
| 48 | +describe('preset.inject()', () => { |
| 49 | + afterEach(() => { |
| 50 | + win?.close(); |
| 51 | + win = undefined; |
| 52 | + }); |
| 53 | + |
| 54 | + test('wraps the preset in the "bongo" layer, so a page rule beats it', () => { |
| 55 | + mount(); |
| 56 | + preset.inject('fade'); |
| 57 | + let [ tag ] = tags(); |
| 58 | + expect(tag).toStartWith('<style data-bongo-preset="fade">@layer bongo{'); |
| 59 | + expect(tag).toEndWith('}</style>'); |
| 60 | + expect(tag).toInclude('::view-transition-old(root){animation-name:bongo-fade-out'); |
| 61 | + }); |
| 62 | + |
| 63 | + test('"slide" is layered too', () => { |
| 64 | + mount(); |
| 65 | + preset.inject('slide'); |
| 66 | + expect(tags()[0]).toInclude('@layer bongo{@keyframes bongo-slide-in'); |
| 67 | + }); |
| 68 | + |
| 69 | + test('"initial" adds no CSS', () => { |
| 70 | + mount(); |
| 71 | + preset.inject('initial'); |
| 72 | + expect(tags()).toEqual([]); |
| 73 | + }); |
| 74 | + |
| 75 | + test('an unknown preset adds no CSS', () => { |
| 76 | + mount(); |
| 77 | + preset.inject('my-custom-preset'); |
| 78 | + expect(tags()).toEqual([]); |
| 79 | + }); |
| 80 | + |
| 81 | + test('a second call adds no second tag', () => { |
| 82 | + mount(); |
| 83 | + preset.inject('fade'); |
| 84 | + preset.inject('fade'); |
| 85 | + expect(tags()).toHaveLength(1); |
| 86 | + }); |
| 87 | +}); |
0 commit comments