Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
connectdotz committed Sep 27, 2021
1 parent 5e16b36 commit 58743df
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/SnapshotCodeLens/SnapshotCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SnapshotCodeLensProvider implements vscode.CodeLensProvider {
}
}

export function registerSnapshotCodeLens(enableSnapshotPreviews: boolean) {
export function registerSnapshotCodeLens(enableSnapshotPreviews: boolean): vscode.Disposable[] {
if (!enableSnapshotPreviews) {
return [];
}
Expand Down
63 changes: 63 additions & 0 deletions tests/SnapshotCodeLens/SnapshotCodeLensProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
jest.unmock('../../src/SnapshotCodeLens/SnapshotCodeLensProvider');

import * as vscode from 'vscode';
import { registerSnapshotCodeLens } from '../../src/SnapshotCodeLens/SnapshotCodeLensProvider';
import { Snapshot } from 'jest-editor-support';

describe('SnapshotCodeLensProvider', () => {
const mockMetadataAsync = jest.fn();
const mockCodeLens = jest.fn();
beforeEach(() => {
jest.resetAllMocks();
(Snapshot as jest.Mocked<any>).mockReturnValue({
getMetadataAsync: mockMetadataAsync,
});
(vscode as jest.Mocked<any>).CodeLens = mockCodeLens;
});
describe('registerSnapshotCodeLens', () => {
it('register if enableSnapshotPreviews is not false', () => {
const registration = registerSnapshotCodeLens(true);
expect(registration.length > 0).toBeTruthy();
expect(vscode.languages.registerCodeLensProvider).toBeCalled();
expect(vscode.commands.registerCommand).toBeCalledWith(
expect.stringContaining('snapshot.missing'),
expect.anything()
);
});
it('not register if enableSnapshotPreviews is false', () => {
const registration = registerSnapshotCodeLens(false);
expect(registration).toHaveLength(0);
expect(vscode.languages.registerCodeLensProvider).not.toBeCalled();
expect(vscode.commands.registerCommand).not.toBeCalled();
});
});
describe('provideCodeLenses', () => {
let provider;
const snapshotMetadata = (line: number, exists = true): any => ({
node: { loc: { start: { line } } },
exists,
});
beforeEach(() => {
registerSnapshotCodeLens(true);
provider = (vscode.languages.registerCodeLensProvider as jest.Mocked<any>).mock.calls[0][1];
});
it('create codeLens for each snapshot', async () => {
mockMetadataAsync.mockReturnValue(
Promise.resolve([snapshotMetadata(10, true), snapshotMetadata(20, false)])
);

await provider.provideCodeLenses({ uri: { fsPath: 'whatever' } }, {});
expect(mockCodeLens).toBeCalledTimes(2);

let [, command] = mockCodeLens.mock.calls[0];
let range = (vscode.Range as jest.Mocked<any>).mock.calls[0];
expect(range).toEqual([9, 0, 9, 0]);
expect(command.title).toEqual('view snapshot');

[, command] = mockCodeLens.mock.calls[1];
range = (vscode.Range as jest.Mocked<any>).mock.calls[1];
expect(range).toEqual([19, 0, 19, 0]);
expect(command.title).toEqual('snapshot missing');
});
});
});

0 comments on commit 58743df

Please sign in to comment.