Difference between revisions of "Jest"

From Wasya Wiki
Jump to: navigation, search
(Created page with " <pre> // const mockPush = jest.fn() // jest.mock('react-router-dom', () => { // const originalModule = jest.requireActual('react-router-dom') // return { // __esModu...")
 
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
  
<pre>
+
    logg(w.find('.BackBtn').exists(), 'found?')
  
 +
== mock router ==
 +
<pre>
 
// const mockPush = jest.fn()
 
// const mockPush = jest.fn()
 
// jest.mock('react-router-dom', () => {
 
// jest.mock('react-router-dom', () => {
Line 15: Line 17:
 
</pre>
 
</pre>
  
 +
// From: https://stackoverflow.com/questions/58392815/how-to-mock-usehistory-hook-in-jest
 
<pre>
 
<pre>
 +
jest.mock('react-router-dom', () => ({
 +
  ...jest.requireActual('react-router-dom'),
 +
  useHistory: () => ({
 +
    goBack: jest.fn()
 +
  })
 +
}));
 +
</pre>
  
 +
 +
== mock api ==
 +
<pre>
 +
jest.mock('$shared/Api')
 
// jest.mock('$shared/Api', () => {
 
// jest.mock('$shared/Api', () => {
 
//  return {
 
//  return {

Latest revision as of 17:47, 23 September 2022

   logg(w.find('.BackBtn').exists(), 'found?')

mock router

// const mockPush = jest.fn()
// jest.mock('react-router-dom', () => {
//   const originalModule = jest.requireActual('react-router-dom')
//   return {
//     __esModule: true,
//     ...originalModule,
//     useHistory: () => ({
//       push: mockPush,
//     }),
//   }
// })

// From: https://stackoverflow.com/questions/58392815/how-to-mock-usehistory-hook-in-jest

jest.mock('react-router-dom', () => ({
  ...jest.requireActual('react-router-dom'),
  useHistory: () => ({
    goBack: jest.fn()
  })
}));


mock api

jest.mock('$shared/Api')
// jest.mock('$shared/Api', () => {
//   return {
//     __esModule: true,
//     default: () => {
//       return {
//         getCity: () => {
//           return new Promise((resolve, reject) => {
//             resolve({
//               data: {
//                 city: {
//                   newsitems: [{ name: 'report-name-2', item_type: 'Report' }]
//                 }
//               }
//             })
//           })
//         },
//       }
//     },
//   }
// })