|
| 1 | +/** |
| 2 | + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +'use strict'; |
| 9 | + |
| 10 | +const TestClass = require('../'); |
| 11 | +const localClass = new TestClass(); |
| 12 | + |
| 13 | +describe('without an explicit restore', () => { |
| 14 | + jest.spyOn(localClass, 'test').mockImplementation(() => 'ABCD'); |
| 15 | + |
| 16 | + test('first test', () => { |
| 17 | + expect(localClass.test()).toEqual('ABCD'); |
| 18 | + expect(localClass.test).toHaveBeenCalledTimes(1); |
| 19 | + }); |
| 20 | + |
| 21 | + test('second test', () => { |
| 22 | + expect(localClass.test()).toEqual('ABCD'); |
| 23 | + expect(localClass.test).toHaveBeenCalledTimes(2); |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +describe('with an explicit restore', () => { |
| 28 | + beforeEach(() => { |
| 29 | + jest.restoreAllMocks(); |
| 30 | + }); |
| 31 | + |
| 32 | + test('first test', () => { |
| 33 | + jest.spyOn(localClass, 'test').mockImplementation(() => 'ABCD'); |
| 34 | + expect(localClass.test()).toEqual('ABCD'); |
| 35 | + expect(localClass.test).toHaveBeenCalledTimes(1); |
| 36 | + }); |
| 37 | + |
| 38 | + test('second test', () => { |
| 39 | + expect(localClass.test()).toEqual('12345'); |
| 40 | + expect(localClass.test.mock).toBe(undefined); |
| 41 | + }); |
| 42 | +}); |
0 commit comments