|
| 1 | +import { beforeEach, describe, expect, vi } from 'vitest'; |
| 2 | +import { default as angularTransformLoader } from './angular-transform.loader'; |
| 3 | +import { NG_RSPACK_SYMBOL_NAME, type NgRspackCompilation } from '../../models'; |
| 4 | +import type { LoaderContext } from '@rspack/core'; |
| 5 | + |
| 6 | +class StyleUrlsResolverMock { |
| 7 | + constructor(private cb: (content: string) => string) {} |
| 8 | + |
| 9 | + resolve(content: string, _: string): string[] { |
| 10 | + return [this.cb(content)]; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +class TemplateUrlsResolverMock { |
| 15 | + constructor(private cb: (content: string) => string) {} |
| 16 | + |
| 17 | + resolve(content: string, _: string): string[] { |
| 18 | + return [this.cb(content)]; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +describe('angular-transform.loader', () => { |
| 23 | + const callback = vi.fn(); |
| 24 | + const addDependency = vi.fn(); |
| 25 | + const typescriptFileCache = new Map<string, string | Buffer>(); |
| 26 | + const _compilation = { |
| 27 | + [NG_RSPACK_SYMBOL_NAME]: () => ({ |
| 28 | + typescriptFileCache, |
| 29 | + }), |
| 30 | + } as unknown as NgRspackCompilation; |
| 31 | + const thisValue = { |
| 32 | + async: vi.fn(() => callback), |
| 33 | + _compilation: {}, |
| 34 | + } as unknown as LoaderContext<unknown>; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + vi.clearAllMocks(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return content when NG_RSPACK_SYMBOL_NAME is undefined', () => { |
| 41 | + angularTransformLoader.call(thisValue, 'content'); |
| 42 | + expect(callback).toHaveBeenCalledWith(null, 'content'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should add dependencies for resolved template and style URLs', () => { |
| 46 | + angularTransformLoader.call( |
| 47 | + { |
| 48 | + ...thisValue, |
| 49 | + _compilation, |
| 50 | + resourcePath: '/home/projects/analog/src/app/app.component.ts', |
| 51 | + addDependency, |
| 52 | + }, |
| 53 | + '@Component()', |
| 54 | + { |
| 55 | + templateUrlsResolver: new TemplateUrlsResolverMock( |
| 56 | + () => |
| 57 | + './app.component.html|/home/projects/analog/src/app/app.component.html' |
| 58 | + ), |
| 59 | + styleUrlsResolver: new StyleUrlsResolverMock( |
| 60 | + () => |
| 61 | + './app.component.css|/home/projects/analog/src/app/app.component.css' |
| 62 | + ), |
| 63 | + } as any |
| 64 | + ); |
| 65 | + |
| 66 | + expect(addDependency).toHaveBeenCalledTimes(2); |
| 67 | + expect(addDependency).toHaveBeenNthCalledWith( |
| 68 | + 1, |
| 69 | + '/home/projects/analog/src/app/app.component.html' |
| 70 | + ); |
| 71 | + expect(addDependency).toHaveBeenNthCalledWith( |
| 72 | + 2, |
| 73 | + '/home/projects/analog/src/app/app.component.css' |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return content when typescriptFileCache does not contain normalizedRequest', () => { |
| 78 | + angularTransformLoader.call( |
| 79 | + { |
| 80 | + ...thisValue, |
| 81 | + _compilation, |
| 82 | + resourcePath: '/path/to/file.ts', |
| 83 | + }, |
| 84 | + 'content' |
| 85 | + ); |
| 86 | + |
| 87 | + expect(callback).toHaveBeenCalledWith(null, 'content'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should return content when typescriptFileCache does contain string', () => { |
| 91 | + typescriptFileCache.set( |
| 92 | + '/home/projects/analog/src/app/app.component.ts', |
| 93 | + 'cached content' |
| 94 | + ); |
| 95 | + |
| 96 | + angularTransformLoader.call( |
| 97 | + { |
| 98 | + ...thisValue, |
| 99 | + _compilation, |
| 100 | + resourcePath: '/home/projects/analog/src/app/app.component.ts', |
| 101 | + }, |
| 102 | + '@Component()' |
| 103 | + ); |
| 104 | + |
| 105 | + expect(callback).toHaveBeenCalledWith(null, 'cached content'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should return content when typescriptFileCache does contain Buffer', () => { |
| 109 | + typescriptFileCache.set( |
| 110 | + '/home/projects/analog/src/app/app.component.ts', |
| 111 | + Buffer.from('cached content') |
| 112 | + ); |
| 113 | + |
| 114 | + angularTransformLoader.call( |
| 115 | + { |
| 116 | + ...thisValue, |
| 117 | + _compilation, |
| 118 | + resourcePath: '/home/projects/analog/src/app/app.component.ts', |
| 119 | + }, |
| 120 | + '@Component()' |
| 121 | + ); |
| 122 | + |
| 123 | + expect(callback).toHaveBeenCalledWith(null, Buffer.from('cached content')); |
| 124 | + }); |
| 125 | +}); |
0 commit comments