|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { execute } from '../../index'; |
| 10 | +import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeKarmaBuilder } from '../setup'; |
| 11 | + |
| 12 | +describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => { |
| 13 | + describe('Option: "scripts"', () => { |
| 14 | + beforeEach(async () => { |
| 15 | + await setupTarget(harness); |
| 16 | + }); |
| 17 | + |
| 18 | + it(`should be able to access non injected script`, async () => { |
| 19 | + await harness.writeFiles({ |
| 20 | + 'src/test.js': `console.log('hello from test script.')`, |
| 21 | + 'src/app/app.component.ts': ` |
| 22 | + import { Component } from '@angular/core'; |
| 23 | +
|
| 24 | + @Component({ |
| 25 | + selector: 'app-root', |
| 26 | + standalone: false, |
| 27 | + template: '<p>Hello World</p>' |
| 28 | + }) |
| 29 | + export class AppComponent { |
| 30 | + loadScript() { |
| 31 | + return new Promise<void>((resolve, reject) => { |
| 32 | + const script = document.createElement('script'); |
| 33 | + script.onload = () => resolve(); |
| 34 | + script.onerror = reject; |
| 35 | + script.src = 'test.js'; |
| 36 | + document.body.appendChild(script); |
| 37 | + }); |
| 38 | + } |
| 39 | + } |
| 40 | + `, |
| 41 | + 'src/app/app.component.spec.ts': ` |
| 42 | + import { TestBed } from '@angular/core/testing'; |
| 43 | + import { AppComponent } from './app.component'; |
| 44 | +
|
| 45 | + describe('AppComponent', () => { |
| 46 | + beforeEach(() => TestBed.configureTestingModule({ |
| 47 | + declarations: [AppComponent] |
| 48 | + })); |
| 49 | +
|
| 50 | + it('should load script', async () => { |
| 51 | + const fixture = TestBed.createComponent(AppComponent); |
| 52 | + fixture.detectChanges(); |
| 53 | +
|
| 54 | + await expectAsync(fixture.componentInstance.loadScript()).toBeResolved(); |
| 55 | + }); |
| 56 | + });`, |
| 57 | + }); |
| 58 | + |
| 59 | + harness.useTarget('test', { |
| 60 | + ...BASE_OPTIONS, |
| 61 | + scripts: [ |
| 62 | + { |
| 63 | + input: 'src/test.js', |
| 64 | + bundleName: 'test', |
| 65 | + inject: false, |
| 66 | + }, |
| 67 | + ], |
| 68 | + }); |
| 69 | + |
| 70 | + const { result } = await harness.executeOnce(); |
| 71 | + expect(result?.success).toBeTrue(); |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments