Skip to content

Commit

Permalink
fix: mock webcontainer api and xterm for unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
cpaulve-1A committed Dec 3, 2024
1 parent 31e5f9f commit 43920a1
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 12 deletions.
9 changes: 9 additions & 0 deletions apps/showcase/src/app/sdk-training/sdk-training.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import {
describe('SdkTrainingComponent', () => {
let component: SdkTrainingComponent;
let fixture: ComponentFixture<SdkTrainingComponent>;
global.fetch = jest.fn(() => Promise.resolve({
ok: true,
json: () => Promise.resolve({}),
headers: {},
redirected: false,
status: 200,
statusText: 'OK',
text: () => Promise.resolve('')
} as Response));

beforeEach(async () => {
await TestBed.configureTestingModule({
Expand Down
10 changes: 10 additions & 0 deletions apps/showcase/src/components/showcase/sdk/sdk-pres.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ describe('SdkPresComponent', () => {
petApiFixture.findPetsByStatus = petApiFixture.findPetsByStatus.mockResolvedValue([]);

beforeEach(() => {
global.fetch = jest.fn(() => Promise.resolve({
ok: true,
json: () => Promise.resolve({}),
headers: {},
redirected: false,
status: 200,
statusText: 'OK',
text: () => Promise.resolve('')
} as Response));

TestBed.configureTestingModule({
imports: [SdkPresComponent],
providers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ElementRef,
EventEmitter,
inject,
type OnDestroy,
Output,
ViewChild,
} from '@angular/core';
Expand All @@ -25,7 +24,7 @@ import {
imports: [],
template: '<div #terminal class="h-100"></div>'
})
export class CodeEditorTerminalComponent implements OnDestroy, AfterViewChecked, AfterViewInit {
export class CodeEditorTerminalComponent implements AfterViewChecked, AfterViewInit {
/**
* Terminal component that can be used as input/output element for a webcontainer process
*/
Expand Down Expand Up @@ -65,7 +64,11 @@ export class CodeEditorTerminalComponent implements OnDestroy, AfterViewChecked,
const disposable = this.terminal.onWriteParsed(() => {
this.terminalActivity.emit();
});
inject(DestroyRef).onDestroy(() => disposable.dispose());
inject(DestroyRef).onDestroy(() => {
disposable.dispose();
this.terminal.dispose();
this.disposed.emit();
});
}

/**
Expand All @@ -89,12 +92,4 @@ export class CodeEditorTerminalComponent implements OnDestroy, AfterViewChecked,
public ngAfterViewChecked() {
this.fitAddon.fit();
}

/**
* @inheritDoc
*/
public ngOnDestroy() {
this.terminal.dispose();
this.disposed.emit();
}
}
11 changes: 10 additions & 1 deletion apps/showcase/src/components/training/training.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ import {
describe('SdkTrainingComponent', () => {
let component: TrainingComponent;
let fixture: ComponentFixture<TrainingComponent>;

beforeEach(async () => {
global.fetch = jest.fn(() => Promise.resolve({
ok: true,
json: () => Promise.resolve({}),
headers: {},
redirected: false,
status: 200,
statusText: 'OK',
text: () => Promise.resolve('')
} as Response));

await TestBed.configureTestingModule({
imports: [TrainingComponent]
})
Expand Down
5 changes: 5 additions & 0 deletions apps/showcase/testing/mocks/webcontainer-api.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class WebContainerApiMock {
public static boot = jest.fn(() => Promise.resolve({
on: jest.fn(() => jest.fn())
}));
}
11 changes: 11 additions & 0 deletions apps/showcase/testing/mocks/x-term.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class XtermTerminalMock {
public loadAddon = jest.fn(() => Promise.resolve());
public open = jest.fn();
public clear = jest.fn();
public kill = jest.fn();
public getWriter = jest.fn();
public dispose = jest.fn();
public onWriteParsed = () => ({
dispose: jest.fn()
});
}
18 changes: 18 additions & 0 deletions apps/showcase/testing/setup-jest.ts
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
import 'jest-preset-angular/setup-jest';
import {
WebContainerApiMock,
} from './mocks/webcontainer-api.mock';
import {
XtermTerminalMock,
} from './mocks/x-term.mock';

jest.mock('@webcontainer/api',
() => ({
WebContainer: WebContainerApiMock
}), {
virtual: true
});

jest.mock('@xterm/xterm',
() => ({ Terminal: XtermTerminalMock }), {
virtual: true
});

0 comments on commit 43920a1

Please sign in to comment.