towerofhope
towerofhope

Reputation: 106

Mocking different values for the same module using Jest

I trying to test a react component using jest, I am new to testing so forgive any mistakes.

I was following this solution Jest mock module multiple times with different values but the problem is that I have a different component structure.

Code : test.tsx

import { AccountPage } from "./AccountPage";
import { Store, Symbol } from "store-provider";
   const accountStore = {
   state: {
     fetching: false,
   };

    <Store stores={[ [Symbol, accountStore] ]}>
      <AccountPage />
    </Store>

Code : Account.tsx

import {module} from './modules';
import profile from '/profile';
export const () => {
 {module['case'] && (
  <Profile />
 )
 <div> Hello </div>
}

Can anyone tell how the can I use render in jest to run this scenarios. In the question that is linked its running as a single returning component.

I want to mock the module['case'] to be true and false. so I can test both cases.

Upvotes: 1

Views: 639

Answers (1)

Estus Flask
Estus Flask

Reputation: 222503

Considering that case is writable object property, it can be just backed up and restored to not affect other tests:

let originalCase;

beforeEach(() => {
   originalCase = module.case;
});

afterEach(() => {
   module.case = originalCase;
});

it('...', () => {
  module.case = true;
  ...
});

it('...', () => {
  module.case = false;
  ...
});

Upvotes: 2

Related Questions