Skip to content

Commit

Permalink
test: added tests for public API
Browse files Browse the repository at this point in the history
  • Loading branch information
rossknudsen committed Sep 29, 2020
1 parent f81e223 commit 431b24d
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 3 deletions.
3 changes: 3 additions & 0 deletions __mocks__/supports-color.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
supportsColor: () => false,
};
2 changes: 1 addition & 1 deletion __mocks__/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const window = {
showWorkspaceFolderPick: jest.fn(),
onDidChangeActiveTextEditor: jest.fn(),
showInformationMessage: jest.fn(),
}
visibleTextEditors: [],

const workspace = {
getConfiguration: jest.fn(),
Expand Down
53 changes: 52 additions & 1 deletion tests/JestExt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const statusBar = {
jest.mock('../src/StatusBar', () => ({ statusBar }));

import { JestExt } from '../src/JestExt';
import { ProjectWorkspace } from 'jest-editor-support';
import { JestTotalResults, ProjectWorkspace } from 'jest-editor-support';
import { window, workspace, debug, ExtensionContext, TextEditorDecorationType } from 'vscode';
import { hasDocument, isOpenInMultipleEditors } from '../src/editor';
import { TestStatus } from '../src/decorations/test-status';
Expand All @@ -33,6 +33,8 @@ import { JestProcessManager, JestProcess } from '../src/JestProcessManagement';
import * as messaging from '../src/messaging';
import { CoverageMapProvider } from '../src/Coverage';
import inlineError from '../src/decorations/inline-error';
import { resultsWithLowerCaseWindowsDriveLetters } from '../src/TestResults';
import { resultsWithoutAnsiEscapeSequence } from '../src/TestResults/TestResult';

/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "expectItTakesNoAction"] }] */

Expand Down Expand Up @@ -804,6 +806,55 @@ describe('JestExt', () => {
});
});

describe('updateWithData', () => {
let sut: JestExt;
const testResultsCallback = jest.fn();

const settings: any = {
debugCodeLens: {},
enableSnapshotUpdateMessages: true,
};

beforeEach(() => {
jest.resetAllMocks();
const projectWorkspace = new ProjectWorkspace(null, null, null, null);
sut = new JestExt(
context,
workspaceFolder,
projectWorkspace,
channelStub,
settings,
debugCodeLensProvider,
debugConfigurationProvider,
null,
null,
null,
testResultsCallback
);

((resultsWithLowerCaseWindowsDriveLetters as unknown) as jest.Mock<
typeof resultsWithLowerCaseWindowsDriveLetters
>).mockImplementationOnce(() => (data: JestTotalResults) => data);
((resultsWithoutAnsiEscapeSequence as unknown) as jest.Mock<
typeof resultsWithoutAnsiEscapeSequence
>).mockImplementationOnce(() => (data: JestTotalResults) => data);
});

it('should call onTestResultsChanged when new test results are available', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
const testResults: JestTotalResults = {
coverageMap: {},
};
// the _updateCoverageMap method is tested elsewhere. We just stub it out.
(sut as any)._updateCoverageMap = () => Promise.resolve();

(sut as any).updateWithData(testResults);

expect(testResultsCallback).toHaveBeenCalled();
});
});

describe('handleJestEditorSupportEvent()', () => {
let sut: JestExt;

Expand Down
13 changes: 12 additions & 1 deletion tests/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ const jestInstance = {
restartProcess: jest.fn(),
};

const publicApi = {};

const extensionManager = {
register: jest.fn(),
getByName: jest.fn().mockReturnValue(jestInstance),
get: jest.fn().mockReturnValue(jestInstance),
getPublicApi: jest.fn(),
getPublicApi: jest.fn().mockReturnValue(publicApi),
unregisterAll: jest.fn(),
registerCommand: jest.fn().mockImplementation((...args) => args),
};
Expand Down Expand Up @@ -116,6 +118,15 @@ describe('Extension', () => {
expect(context.subscriptions.push.mock.calls[0]).toContain('onDidChangeWorkspaceFolders');
});

it('should return ExtensionManager.getPublicApi', () => {
extensionManager.getPublicApi.mockClear();

const api = activate(context);

expect(extensionManager.getPublicApi).toHaveBeenCalledTimes(1);
expect(api).toBe(publicApi);
});

describe('should register a command', () => {
beforeEach(() => {
jestInstance.toggleCoverageOverlay.mockReset();
Expand Down
76 changes: 76 additions & 0 deletions tests/extensionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,80 @@ describe('InstancesManager', () => {
});
});
});

describe('getPublicApi()', () => {
const callbackMock = jest.fn();
let api: any;
beforeEach(() => {
callbackMock.mockClear();

// grab a reference to the public API each time the extension is recreated.
api = extensionManager.getPublicApi();
});

it('should return an object with a "subscribeToTestResults" method on it', () => {
expect(typeof api.subscribeToTestResults).toBe('function');

const unsubscribe = api.subscribeToTestResults(callbackMock);

expect(callbackMock).not.toBeCalled();
expect(typeof unsubscribe).toBe('function');
});

describe('subscribeToTestResults()', () => {
const fakeTestResults = {};
let notifyNewTestResults: () => void;

beforeEach(() => {
// pretend that an instance of JestExt has called to JestExtension with test results.
// alternative ways this could work, is to receive the constructor parameters to the
// fake JestExt and invoke the callback there.
notifyNewTestResults = () =>
(extensionManager as any).onTestResultsChanged(fakeTestResults);
});

it('should not throw an error if the callback throws', () => {
callbackMock.mockImplementationOnce(() => {
throw new Error();
});

api.subscribeToTestResults(callbackMock);
notifyNewTestResults();

expect(callbackMock).toBeCalledTimes(1);
expect(callbackMock).toBeCalledWith(fakeTestResults);
});

it('should invoke subscribe callback when new test results are available', () => {
api.subscribeToTestResults(callbackMock);

notifyNewTestResults();

expect(callbackMock).toBeCalledTimes(1);
expect(callbackMock).toBeCalledWith(fakeTestResults);
});

it('should invoke multiple subscribers when new test results are available', () => {
api.subscribeToTestResults(callbackMock);
const secondSubscriber = jest.fn();
api.subscribeToTestResults(secondSubscriber);

notifyNewTestResults();

expect(callbackMock).toBeCalledTimes(1);
expect(callbackMock).toBeCalledWith(fakeTestResults);
expect(secondSubscriber).toBeCalledTimes(1);
expect(secondSubscriber).toBeCalledWith(fakeTestResults);
});

it('should not invoke subscribe callback after unsubscribe', () => {
const unsubscribe = api.subscribeToTestResults(callbackMock);

unsubscribe();
notifyNewTestResults();

expect(callbackMock).not.toBeCalled();
});
});
});
});

0 comments on commit 431b24d

Please sign in to comment.