react-testing-library

How to mock interceptors when using jest.mock('axios')?

随声附和 提交于 2020-03-24 09:45:43
问题 When running a test using jest I have the basic test suit syntax: jest.mock('axios'); describe('app', () => { let render beforeEach(() => { axiosMock.get.mockResolvedValueOnce({ data: {greeting: 'hello there'}, }), render= renderApp() }); test('should render something', () => { expect(something).toBeInTheDocument(); }); }); The problem is I have interceptors in my code which when running the test with jest command outputs: TypeError: Cannot read property 'interceptors' of undefined and points

Testing a function that returns an object with a function - JavaScript (Jest)

别说谁变了你拦得住时间么 提交于 2020-03-05 06:02:11
问题 I have a situation where I am facing an issue regarding unit testing of a function. There is a function SOP3loginConfig which returns an object, within that object we have a function isSOP3 that returns a boolean, I want to test this function and cover the test part of it. The actual implementation of the function sop3login.ts export const SOP3loginConfig = (props: IVariables) => { const { i18n } = props; return { buttonLabel: props.user != null ? i18n('StartJourney') : i18n('logIn'),

How to set focus to a div element in a react component using useEffect and useRef hooks? And how to test it?

£可爱£侵袭症+ 提交于 2020-03-03 07:55:32
问题 All most all the articles and examples about setting focus are explained using input element. It works well with input element but how to set focus to a div element using React useRef and useEffect hooks? How to test it using toHaveFocus of react-testing-library ? 回答1: After searching for a while and wasting quite a bit of time, I was able to do it. Sharing it here for friends!! Component file SetFocusToDivComponent.js import React, { useRef, useEffect } from 'react'; export default function

moduleDirectories key does not make it possible to import my test utils

你离开我真会死。 提交于 2020-02-25 06:24:09
问题 I want to test my Expo React Native app with Jest and @testing-lib/react-native . I have the following set up in my package.json . "jest": { "preset": "jest-expo", "moduleDirectories": [ "node_modules", "test-utils" ] }, And my folder structure looks like this: ├──node_modules/ ├──test-utils/ ├──src/ └──package.json src/ contains the test files. I'm testing my configuration with this simple test at src/index.test.js : import { assert } from 'test-utils'; const sum = (a, b) => a + b; describe(

moduleDirectories key does not make it possible to import my test utils

巧了我就是萌 提交于 2020-02-25 06:17:49
问题 I want to test my Expo React Native app with Jest and @testing-lib/react-native . I have the following set up in my package.json . "jest": { "preset": "jest-expo", "moduleDirectories": [ "node_modules", "test-utils" ] }, And my folder structure looks like this: ├──node_modules/ ├──test-utils/ ├──src/ └──package.json src/ contains the test files. I'm testing my configuration with this simple test at src/index.test.js : import { assert } from 'test-utils'; const sum = (a, b) => a + b; describe(

React testing library: The given element does not have a value setter when fireEvent change on input form

好久不见. 提交于 2020-02-21 12:38:13
问题 I want to change the value of material UI TextField in react testing library. I already set up the data-testid. Then using getByTestId i picked up the input element. // the component <TextField data-testid="input-email" variant="outlined" margin="normal" required fullWidth id="email" label="Email Address" name="email" value={email} onChange={e => setEmail(e.target.value)} autoComplete="email" autoFocus /> // the test //... let userInput = getByTestId('input-email') fireEvent.change(userInput,

React testing library: The given element does not have a value setter when fireEvent change on input form

独自空忆成欢 提交于 2020-02-21 12:35:11
问题 I want to change the value of material UI TextField in react testing library. I already set up the data-testid. Then using getByTestId i picked up the input element. // the component <TextField data-testid="input-email" variant="outlined" margin="normal" required fullWidth id="email" label="Email Address" name="email" value={email} onChange={e => setEmail(e.target.value)} autoComplete="email" autoFocus /> // the test //... let userInput = getByTestId('input-email') fireEvent.change(userInput,

Writing test case for rendering post list

假如想象 提交于 2020-02-07 01:58:51
问题 I am wanting to write a test for the code below which basically renders all the blog posts such that the latest post is on the top. I am new to React test Library and every time I add my components in the React testing Library I get the following error : No overload matches this call. Overload 1 of 2, '(props: Readonly): PostList', gave the following error. Property 'posts' is missing in type '{}' but required in type 'Readonly'. Overload 2 of 2, '(props: State, context?: any): PostList',

Why does react hook throw the act error when used with fetch api?

大兔子大兔子 提交于 2020-02-03 05:02:09
问题 I keep getting Warning: An update to App inside a test was not wrapped in act(...). in my test suite whenever I make an API request and update the state. I'm making use of react-testing-library. I also tried using ReactDOM test utils, got the same result. One other thing I tried was wrapping the container in act , still got the same result. Please note that: My App works and my test passes. I just need to know what I was doing wrong or if it's a bug in the react-dom package that's making that

Wrapping async moxios call in act callback

ⅰ亾dé卋堺 提交于 2020-01-25 09:16:07
问题 I am trying to test a react functional component using hooks. The useEffect hook makes a call to a third part API which then calls setState on return. I have the test working but keep getting a warning that an update to the component was not wrapped in act. The problem I have is that the expectation is inside a moxios.wait promise and therefore I cannot wrap that in an act function and then assert on the result of that. The test passes but I know not wrapping code that updates state in an act