Skip to content

Commit

Permalink
Bug-13143-001 Added tests for function exports
Browse files Browse the repository at this point in the history
  • Loading branch information
staplespeter committed Oct 14, 2022
1 parent c36a196 commit 4f3f8db
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/jest-mock/src/__tests__/__fixtures__/class-mocks-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,16 @@ export default class SuperTestClass {
}

export class TestClass extends SuperTestClass {}

export function testFunction1() {
return 'testFunction1';
}

function testFunction() {
return 'testFunction2';
}
export const testFunction2 = testFunction;

export const testFunction3 = () => {
return 'testFunction3';
};
5 changes: 5 additions & 0 deletions packages/jest-mock/src/__tests__/__fixtures__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const f1 = () => {
console.log("Im f1 calling f2");
f2();
};
export const f2 = () => console.log("Im f2");
31 changes: 31 additions & 0 deletions packages/jest-mock/src/__tests__/class-mocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,37 @@
* LICENSE file in the root directory of this source tree.
*
*/
import * as testTypes from './__fixtures__/class-mocks-types';
import * as m from './__fixtures__/index';

describe('Testing the mocking of exported functions', () => {
test('adds 1 + 2 to equal 3', () => {
const spy = jest.spyOn(m, 'f2').mockImplementationOnce(jest.fn());
m.f2();
expect(spy).toHaveBeenCalled();
});

it('can mock a directly exported function', () => {
jest.spyOn(testTypes, 'testFunction1').mockImplementation(() => {
return 'mockTestFunction';
});
expect(testTypes.testFunction1()).toBe('mockTestFunction');
});

it('can mock an indirectly exported function', () => {
jest.spyOn(testTypes, 'testFunction2').mockImplementation(() => {
return 'mockTestFunction';
});
expect(testTypes.testFunction2()).toBe('mockTestFunction');
});

it('can mock an indirectly exported anonymous function', () => {
jest.spyOn(testTypes, 'testFunction3').mockImplementation(() => {
return 'mockTestFunction';
});
expect(testTypes.testFunction3()).toBe('mockTestFunction');
});
});

describe('Testing the mocking of a class', () => {
it('can call an instance method', () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/jest-mock/src/__tests__/index2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as m from './__fixtures__/index';

test('adds 1 + 2 to equal 3', () => {
const spy = jest.spyOn(m, 'f2').mockImplementationOnce(jest.fn());
m.f2();
expect(spy).toHaveBeenCalled();
});

0 comments on commit 4f3f8db

Please sign in to comment.