|
| 1 | +import { ApplicationRef } from '@angular/core'; |
| 2 | +import { TestBed } from '@angular/core/testing'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { contentFilesResource } from './content-files-resource'; |
| 6 | +import { CONTENT_FILES_LIST_TOKEN } from '../../src/lib/content-files-list-token'; |
| 7 | +import { |
| 8 | + CONTENT_LIST_LOADER, |
| 9 | + withContentListLoader, |
| 10 | +} from '../../src/lib/content-list-loader'; |
| 11 | +import { RenderTaskService } from '../../src/lib/render-task.service'; |
| 12 | + |
| 13 | +describe('contentFilesResource', () => { |
| 14 | + it('runs the list loader again on reload', async () => { |
| 15 | + const files = [ |
| 16 | + { filename: '/src/content/a.md', slug: 'a', attributes: {} }, |
| 17 | + { filename: '/src/content/b.md', slug: 'b', attributes: {} }, |
| 18 | + ]; |
| 19 | + const loader = vi |
| 20 | + .fn() |
| 21 | + .mockRejectedValueOnce(new Error('offline')) |
| 22 | + .mockResolvedValue(files); |
| 23 | + TestBed.configureTestingModule({ |
| 24 | + providers: [{ provide: CONTENT_LIST_LOADER, useValue: loader }], |
| 25 | + }); |
| 26 | + |
| 27 | + const result = TestBed.runInInjectionContext(() => |
| 28 | + contentFilesResource((file) => file.slug === 'b'), |
| 29 | + ); |
| 30 | + await settle(); |
| 31 | + |
| 32 | + expect(result.error()?.message).toBe('offline'); |
| 33 | + |
| 34 | + result.reload(); |
| 35 | + await settle(); |
| 36 | + |
| 37 | + expect(loader).toHaveBeenCalledTimes(2); |
| 38 | + expect(result.value()).toEqual([files[1]]); |
| 39 | + }); |
| 40 | + |
| 41 | + it('loads through withContentListLoader outside an injection context', async () => { |
| 42 | + const files = [ |
| 43 | + { filename: '/src/content/a.md', slug: 'a', attributes: {} }, |
| 44 | + ]; |
| 45 | + TestBed.configureTestingModule({ |
| 46 | + providers: [ |
| 47 | + withContentListLoader(), |
| 48 | + RenderTaskService, |
| 49 | + { provide: CONTENT_FILES_LIST_TOKEN, useValue: files }, |
| 50 | + ], |
| 51 | + }); |
| 52 | + |
| 53 | + const result = TestBed.runInInjectionContext(() => contentFilesResource()); |
| 54 | + await settle(); |
| 55 | + |
| 56 | + expect(result.value()).toEqual(files); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +async function settle() { |
| 61 | + TestBed.tick(); |
| 62 | + await TestBed.inject(ApplicationRef).whenStable(); |
| 63 | +} |
0 commit comments