-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added widget template with unit tests
fixed #103 Contributed on behalf of STMicroelectronics Signed-off-by: Jonas Helming <[email protected]>
- Loading branch information
1 parent
794ad7d
commit e75778e
Showing
7 changed files
with
155 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
templates/widget/__tests__/mock-objects/mock-message-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { injectable, inject } from 'inversify'; | ||
import { Message, MessageClient, MessageOptions, MessageType, ProgressMessage, ProgressUpdate } from '@theia/core/lib/common/message-service-protocol'; | ||
import { CancellationToken, MessageService } from '@theia/core'; | ||
|
||
@injectable() | ||
export class MockMessageService extends MessageService{ | ||
|
||
constructor( | ||
@inject(MessageClient) protected readonly client: MessageClient | ||
) { super(client) } | ||
|
||
logWasCalled: boolean = false; | ||
infoWasCalled: boolean = false; | ||
warnWasCalled: boolean = false; | ||
errorWasCalled: boolean = false; | ||
|
||
log<T extends string>(message: string, ...actions: T[]): Promise<T | undefined>; | ||
log<T extends string>(message: string, options?: MessageOptions, ...actions: T[]): Promise<T | undefined>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
log(message: string, ...args: any[]): Promise<string | undefined> { | ||
this.logWasCalled = true; | ||
return this.processMessage(MessageType.Log, message, args); | ||
} | ||
|
||
info<T extends string>(message: string, ...actions: T[]): Promise<T | undefined>; | ||
|
||
info<T extends string>(message: string, options?: MessageOptions, ...actions: T[]): Promise<T | undefined>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
info(message: string, ...args: any[]): Promise<string | undefined> { | ||
this.infoWasCalled = true; | ||
return this.processMessage(MessageType.Info, message, args); | ||
} | ||
|
||
warn<T extends string>(message: string, ...actions: T[]): Promise<T | undefined>; | ||
warn<T extends string>(message: string, options?: MessageOptions, ...actions: T[]): Promise<T | undefined>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
warn(message: string, ...args: any[]): Promise<string | undefined> { | ||
this.warnWasCalled = true; | ||
return this.processMessage(MessageType.Warning, message, args); | ||
} | ||
|
||
error<T extends string>(message: string, ...actions: T[]): Promise<T | undefined>; | ||
error<T extends string>(message: string, options?: MessageOptions, ...actions: T[]): Promise<T | undefined>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
error(message: string, ...args: any[]): Promise<string | undefined> { | ||
this.errorWasCalled = true; | ||
return this.processMessage(MessageType.Error, message, args); | ||
} | ||
} | ||
|
||
@injectable() | ||
export class MockMessageClient { | ||
|
||
showMessage(message: Message): Promise<string | undefined> { | ||
return Promise.resolve(undefined); | ||
} | ||
|
||
showProgress(progressId: string, message: ProgressMessage, cancellationToken: CancellationToken): Promise<string | undefined> { | ||
return Promise.resolve(undefined); | ||
} | ||
reportProgress(progressId: string, update: ProgressUpdate, message: ProgressMessage, cancellationToken: CancellationToken): Promise<void> { | ||
return Promise.resolve(undefined); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'reflect-metadata'; | ||
import { MessageClient, MessageService } from '@theia/core'; | ||
import { ContainerModule, Container } from 'inversify'; | ||
import { MockMessageClient, MockMessageService } from './mock-objects/mock-message-service'; | ||
import { <%= params.extensionPrefix %>Widget } from '../<%= params.extensionPath %>-widget'; | ||
import { render } from '@testing-library/react' | ||
|
||
describe('widget extension unit tests', () => { | ||
|
||
let widget: any; | ||
|
||
beforeEach(async () => { | ||
const module = new ContainerModule( bind => { | ||
bind(MessageClient).to(MockMessageClient).inSingletonScope(); | ||
bind(MessageService).to(MockMessageService).inSingletonScope(); | ||
bind(<%= params.extensionPrefix %>Widget).toSelf(); | ||
}); | ||
const container = new Container(); | ||
container.load(module); | ||
widget = container.resolve<<%= params.extensionPrefix %>Widget>(<%= params.extensionPrefix %>Widget); | ||
}); | ||
|
||
it('should render react node correctly', async () => { | ||
const element = render(widget.render()); | ||
expect(element.queryByText('Display Message')).toBeTruthy(); | ||
}); | ||
|
||
it('should inject the message service', async () => { | ||
widget.displayMessage(); | ||
expect(widget.messageService.infoWasCalled).toBe(true); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Config } from '@jest/types'; | ||
|
||
export default async (): Promise<Config.InitialOptions> => ({ | ||
preset: 'ts-jest', | ||
testMatch: ['**.test.ts'], | ||
rootDir: '../', | ||
transform: { | ||
'^.+\\.(ts)$': 'ts-jest', | ||
} | ||
}); |