Skip to content

Instantly share code, notes, and snippets.

@pixelkritzel
Created July 27, 2020 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelkritzel/641da25c52a25ac3a1c9b0c1e9ef1a74 to your computer and use it in GitHub Desktop.
Save pixelkritzel/641da25c52a25ac3a1c9b0c1e9ef1a74 to your computer and use it in GitHub Desktop.
import React from 'react';
import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { mocked } from 'ts-jest/utils';
import { App } from './App';
import { store } from './store';
jest.mock('./store', () => ({
store: {
create() {
return {
counter: 50,
increment: jest.fn().mockImplementation(() => console.log('Running mock increment')),
decrement: jest.fn().mockImplementation(() => console.log('Running mock decrement')),
};
},
},
}));
beforeAll(() => (mockedStore = mocked(store, true).create()));
let mockedStore = mocked(store, true).create();
test('The increment Button to call store.increment', () => {
const { getByText } = render(<App />);
const button = getByText('+');
userEvent.click(button);
expect(mockedStore.increment).toBeCalled();
});
test('The decrement Button to call store.decrement', () => {
const { getByText } = render(<App />);
const button = getByText('-');
userEvent.click(button);
expect(mockedStore.decrement).toBeCalled();
});
test('The counter value is displayed in the component', () => {
const { getByTestId } = render(<App />);
const output = getByTestId('output');
expect(output).toHaveTextContent(mockedStore.counter.toString());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment