|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { expect, test } from 'vitest'; |
| 3 | +import { render, screen } from '../public_api'; |
| 4 | + |
| 5 | +@Component({ |
| 6 | + selector: 'atl-child', |
| 7 | + template: `Hello from child`, |
| 8 | + standalone: true, |
| 9 | +}) |
| 10 | +class ChildComponent {} |
| 11 | + |
| 12 | +@Component({ |
| 13 | + selector: 'atl-child', |
| 14 | + template: `Hello from stub`, |
| 15 | + standalone: true, |
| 16 | + host: { 'collision-id': 'StubComponent' }, |
| 17 | +}) |
| 18 | +class StubChildComponent {} |
| 19 | + |
| 20 | +@Component({ |
| 21 | + selector: 'atl-other', |
| 22 | + template: `Hello from other`, |
| 23 | + standalone: true, |
| 24 | +}) |
| 25 | +class OtherComponent {} |
| 26 | + |
| 27 | +@Component({ |
| 28 | + selector: 'atl-fixture', |
| 29 | + template: `<atl-child /><atl-other />`, |
| 30 | + standalone: true, |
| 31 | + imports: [ChildComponent, OtherComponent], |
| 32 | +}) |
| 33 | +class FixtureComponent {} |
| 34 | + |
| 35 | +@Component({ |
| 36 | + selector: 'atl-non-standalone', |
| 37 | + template: `non-standalone`, |
| 38 | + standalone: false, |
| 39 | +}) |
| 40 | +class NonStandaloneComponent {} |
| 41 | + |
| 42 | +test('importOverrides - replaces a single import', async () => { |
| 43 | + await render(FixtureComponent, { |
| 44 | + importOverrides: [{ replace: ChildComponent, with: StubChildComponent }], |
| 45 | + }); |
| 46 | + |
| 47 | + expect(screen.getByText('Hello from stub')).toBeInTheDocument(); |
| 48 | + expect(screen.queryByText('Hello from child')).not.toBeInTheDocument(); |
| 49 | +}); |
| 50 | + |
| 51 | +test('importOverrides - leaves other imports intact', async () => { |
| 52 | + await render(FixtureComponent, { |
| 53 | + importOverrides: [{ replace: ChildComponent, with: StubChildComponent }], |
| 54 | + }); |
| 55 | + |
| 56 | + expect(screen.getByText('Hello from stub')).toBeInTheDocument(); |
| 57 | + expect(screen.getByText('Hello from other')).toBeInTheDocument(); |
| 58 | +}); |
| 59 | + |
| 60 | +test('importOverrides - throws on non-standalone component', async () => { |
| 61 | + await expect( |
| 62 | + render(NonStandaloneComponent, { |
| 63 | + declarations: [NonStandaloneComponent], |
| 64 | + excludeComponentDeclaration: true, |
| 65 | + importOverrides: [{ replace: ChildComponent, with: StubChildComponent }], |
| 66 | + } as any), |
| 67 | + ).rejects.toThrow(/Cannot specify importOverrides on a template or non-standalone component/); |
| 68 | +}); |
| 69 | + |
| 70 | +test('importOverrides - throws when used with componentImports', async () => { |
| 71 | + await expect( |
| 72 | + render(FixtureComponent, { |
| 73 | + componentImports: [ChildComponent], |
| 74 | + importOverrides: [{ replace: ChildComponent, with: StubChildComponent }], |
| 75 | + }), |
| 76 | + ).rejects.toThrow(/Cannot specify both componentImports and importOverrides/); |
| 77 | +}); |
| 78 | + |
| 79 | +test('importOverrides - empty array is a no-op', async () => { |
| 80 | + await render(FixtureComponent, { |
| 81 | + importOverrides: [], |
| 82 | + }); |
| 83 | + |
| 84 | + expect(screen.getByText('Hello from child')).toBeInTheDocument(); |
| 85 | + expect(screen.getByText('Hello from other')).toBeInTheDocument(); |
| 86 | +}); |
0 commit comments