|
| 1 | +import { preloadAssets } from '@tarojs/taro-h5' |
| 2 | + |
| 3 | +describe('preloadAssets', () => { |
| 4 | + const links = () => Array.from(document.querySelectorAll<HTMLLinkElement>('link[rel="preload"]')) |
| 5 | + beforeEach(() => jest.spyOn(DOMTokenList.prototype, 'supports').mockReturnValue(true)) |
| 6 | + afterEach(() => { |
| 7 | + links().forEach(link => link.remove()) |
| 8 | + jest.restoreAllMocks() |
| 9 | + }) |
| 10 | + |
| 11 | + it('fails unsupported preloads without inserting or retaining a request', async () => { |
| 12 | + jest.spyOn(DOMTokenList.prototype, 'supports').mockReturnValue(false) |
| 13 | + const fail = jest.fn() |
| 14 | + const complete = jest.fn() |
| 15 | + const options = { data: [{ type: 'image' as const, src: '/unsupported.png' }], fail, complete } |
| 16 | + await preloadAssets(options) |
| 17 | + expect(fail).toHaveBeenCalledTimes(1) |
| 18 | + expect(complete).toHaveBeenCalledTimes(1) |
| 19 | + expect(links()).toHaveLength(0) |
| 20 | + jest.spyOn(DOMTokenList.prototype, 'supports').mockReturnValue(true) |
| 21 | + const retry = preloadAssets(options) |
| 22 | + expect(links()).toHaveLength(1) |
| 23 | + links()[0].dispatchEvent(new Event('load')) |
| 24 | + await retry |
| 25 | + }) |
| 26 | + |
| 27 | + it('preloads images and anonymous fonts and shares concurrent requests', async () => { |
| 28 | + const first = preloadAssets({ data: [{ type: 'image', src: '/hero.png' }, { type: 'font', src: '/font.woff2' }] }) |
| 29 | + const second = preloadAssets({ data: [{ type: 'image', src: 'http://localhost/hero.png' }] }) |
| 30 | + const [image, font] = links() |
| 31 | + expect(links()).toHaveLength(2) |
| 32 | + expect(image.as).toBe('image') |
| 33 | + expect(font.as).toBe('font') |
| 34 | + expect(font.crossOrigin).toBe('anonymous') |
| 35 | + |
| 36 | + image.dispatchEvent(new Event('load')) |
| 37 | + font.dispatchEvent(new Event('load')) |
| 38 | + await expect(first).resolves.toMatchObject({ errMsg: 'preloadAssets:ok' }) |
| 39 | + await expect(second).resolves.toMatchObject({ errMsg: 'preloadAssets:ok' }) |
| 40 | + expect(links()).toHaveLength(0) |
| 41 | + }) |
| 42 | + |
| 43 | + it('cleans up consecutive batches and lets a completed URL be preloaded again', async () => { |
| 44 | + for (let batch = 0; batch < 4; batch++) { |
| 45 | + const data = Array.from({ length: 25 }, (_, index) => ({ |
| 46 | + type: 'image' as const, |
| 47 | + src: `/batch-${batch}-${index}.png`, |
| 48 | + })) |
| 49 | + const request = preloadAssets({ data }) |
| 50 | + expect(links()).toHaveLength(25) |
| 51 | + links().forEach(link => link.dispatchEvent(new Event('load'))) |
| 52 | + await request |
| 53 | + expect(links()).toHaveLength(0) |
| 54 | + } |
| 55 | + |
| 56 | + const repeat = preloadAssets({ data: [{ type: 'image', src: '/batch-0-0.png' }] }) |
| 57 | + expect(links()).toHaveLength(1) |
| 58 | + links()[0].dispatchEvent(new Event('load')) |
| 59 | + await expect(repeat).resolves.toMatchObject({ errMsg: 'preloadAssets:ok' }) |
| 60 | + expect(links()).toHaveLength(0) |
| 61 | + }) |
| 62 | + |
| 63 | + it('reports a failed asset and permits retry', async () => { |
| 64 | + const options = { data: [{ type: 'image' as const, src: '/retry.png' }] } |
| 65 | + const failed = preloadAssets(options) |
| 66 | + links()[0].dispatchEvent(new Event('error')) |
| 67 | + await expect(failed).rejects.toMatchObject({ errMsg: expect.stringContaining('retry.png') }) |
| 68 | + expect(links()).toHaveLength(0) |
| 69 | + const retry = preloadAssets(options) |
| 70 | + links()[0].dispatchEvent(new Event('load')) |
| 71 | + await expect(retry).resolves.toMatchObject({ errMsg: 'preloadAssets:ok' }) |
| 72 | + }) |
| 73 | + |
| 74 | + it('rejects malformed entries without scheduling a preload', async () => { |
| 75 | + await expect(preloadAssets({ data: [{ type: 'image', src: '' }] })).rejects.toMatchObject({ |
| 76 | + errMsg: expect.stringContaining('preloadAssets:fail'), |
| 77 | + }) |
| 78 | + expect(links()).toHaveLength(0) |
| 79 | + }) |
| 80 | + |
| 81 | + it('calls fail and complete once even when multiple assets fail', async () => { |
| 82 | + const fail = jest.fn() |
| 83 | + const complete = jest.fn() |
| 84 | + const success = jest.fn() |
| 85 | + const request = preloadAssets({ |
| 86 | + data: [{ type: 'font', src: '/missing.woff2' }, { type: 'image', src: '/missing.png' }], |
| 87 | + fail, |
| 88 | + complete, |
| 89 | + success, |
| 90 | + }) |
| 91 | + links().forEach(link => link.dispatchEvent(new Event('error'))) |
| 92 | + const result = await request |
| 93 | + expect(fail).toHaveBeenCalledTimes(1) |
| 94 | + expect(fail).toHaveBeenCalledWith(result) |
| 95 | + expect(complete).toHaveBeenCalledTimes(1) |
| 96 | + expect(complete).toHaveBeenCalledWith(result) |
| 97 | + expect(success).not.toHaveBeenCalled() |
| 98 | + expect(links()).toHaveLength(0) |
| 99 | + }) |
| 100 | +}) |
0 commit comments