Skip to content

Commit

Permalink
refactor: optimize unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-deriv committed Jul 1, 2024
1 parent 2f80792 commit c8048df
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,24 @@ jest.mock('@deriv/hooks', () => ({
}));

describe('AfterSignupFlow', () => {
const mockDefault = mockStore({});
const mockedOpenRealAccountSignup = jest.fn();
const mockedSetIsFromSignupAccount = jest.fn();
const mockedSetIsSetupRealAccountOrGoToDemoModalVisible = jest.fn();

const mockDefault = mockStore({
client: {
is_logged_in: true,
has_active_real_account: false,
},
ui: {
is_from_signup_account: true,
openRealAccountSignup: mockedOpenRealAccountSignup,
setIsFromSignupAccount: mockedSetIsFromSignupAccount,
},
traders_hub: {
setIsSetupRealAccountOrGoToDemoModalVisible: mockedSetIsSetupRealAccountOrGoToDemoModalVisible,
},
});

const wrapper = (mock: ReturnType<typeof mockStore> = mockDefault) => {
const Component = ({ children }: { children: JSX.Element }) => (
Expand All @@ -20,37 +37,15 @@ describe('AfterSignupFlow', () => {
return Component;
};

it('should render correctly', () => {
const { container } = render(<AfterSignupFlow />, {
wrapper: wrapper(),
});
expect(container).toBeInTheDocument();
afterEach(() => {
jest.clearAllMocks();
});

it('check if growthbook feature flags are false', () => {
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([false, true]);

const mockedOpenRealAccountSignup = jest.fn();
const mockedSetIsFromSignupAccount = jest.fn();
const mockedSetIsSetupRealAccountOrGoToDemoModalVisible = jest.fn();

const mock = mockStore({
client: {
is_logged_in: true,
has_active_real_account: false,
},
ui: {
is_from_signup_account: true,
openRealAccountSignup: mockedOpenRealAccountSignup,
setIsFromSignupAccount: mockedSetIsFromSignupAccount,
},
traders_hub: {
setIsSetupRealAccountOrGoToDemoModalVisible: mockedSetIsSetupRealAccountOrGoToDemoModalVisible,
},
});

render(<AfterSignupFlow />, {
wrapper: wrapper(mock),
wrapper: wrapper(),
});

expect(mockedOpenRealAccountSignup).not.toBeCalled();
Expand All @@ -62,27 +57,8 @@ describe('AfterSignupFlow', () => {
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValueOnce([true, true]);
(useContentFlag as jest.Mock).mockReturnValue({ is_cr_demo: true, is_eu_demo: false });

const mockedOpenRealAccountSignup = jest.fn();
const mockedSetIsFromSignupAccount = jest.fn();
const mockedSetIsSetupRealAccountOrGoToDemoModalVisible = jest.fn();

const mock = mockStore({
client: {
is_logged_in: true,
has_active_real_account: false,
},
ui: {
is_from_signup_account: true,
openRealAccountSignup: mockedOpenRealAccountSignup,
setIsFromSignupAccount: mockedSetIsFromSignupAccount,
},
traders_hub: {
setIsSetupRealAccountOrGoToDemoModalVisible: mockedSetIsSetupRealAccountOrGoToDemoModalVisible,
},
});

render(<AfterSignupFlow />, {
wrapper: wrapper(mock),
wrapper: wrapper(),
});

expect(mockedOpenRealAccountSignup).toBeCalled();
Expand All @@ -94,27 +70,8 @@ describe('AfterSignupFlow', () => {
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValueOnce([true, true]);
(useContentFlag as jest.Mock).mockReturnValue({ is_cr_demo: false, is_eu_demo: true });

const mockedOpenRealAccountSignup = jest.fn();
const mockedSetIsFromSignupAccount = jest.fn();
const mockedSetIsSetupRealAccountOrGoToDemoModalVisible = jest.fn();

const mock = mockStore({
client: {
is_logged_in: true,
has_active_real_account: false,
},
ui: {
is_from_signup_account: true,
openRealAccountSignup: mockedOpenRealAccountSignup,
setIsFromSignupAccount: mockedSetIsFromSignupAccount,
},
traders_hub: {
setIsSetupRealAccountOrGoToDemoModalVisible: mockedSetIsSetupRealAccountOrGoToDemoModalVisible,
},
});

render(<AfterSignupFlow />, {
wrapper: wrapper(mock),
wrapper: wrapper(),
});

expect(mockedOpenRealAccountSignup).toBeCalled();
Expand All @@ -126,27 +83,8 @@ describe('AfterSignupFlow', () => {
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValueOnce([false, true]);
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValueOnce([true, true]);

const mockedOpenRealAccountSignup = jest.fn();
const mockedSetIsFromSignupAccount = jest.fn();
const mockedSetIsSetupRealAccountOrGoToDemoModalVisible = jest.fn();

const mock = mockStore({
client: {
is_logged_in: true,
has_active_real_account: false,
},
ui: {
is_from_signup_account: true,
openRealAccountSignup: mockedOpenRealAccountSignup,
setIsFromSignupAccount: mockedSetIsFromSignupAccount,
},
traders_hub: {
setIsSetupRealAccountOrGoToDemoModalVisible: mockedSetIsSetupRealAccountOrGoToDemoModalVisible,
},
});

render(<AfterSignupFlow />, {
wrapper: wrapper(mock),
wrapper: wrapper(),
});

expect(mockedOpenRealAccountSignup).not.toBeCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,22 @@ jest.mock('@deriv-com/analytics', () => ({
}));

describe('SetupRealAccountOrGoToDemoModalContent', () => {
const mockDefault = mockStore({});
const mockedOpenRealAccountSignup = jest.fn();
const mockedSetIsFromSignupAccount = jest.fn();
const mockedSetIsSetupRealAccountOrGoToDemoModalVisible = jest.fn();
const mockTrack = Analytics.trackEvent;

const mockDefault = mockStore({
ui: {
is_from_signup_account: true,
openRealAccountSignup: mockedOpenRealAccountSignup,
setIsFromSignupAccount: mockedSetIsFromSignupAccount,
},
traders_hub: {
is_setup_real_account_or_go_to_demo_modal_visible: true,
setIsSetupRealAccountOrGoToDemoModalVisible: mockedSetIsSetupRealAccountOrGoToDemoModalVisible,
},
});

const wrapper = (mock: ReturnType<typeof mockStore> = mockDefault) => {
const Component = ({ children }: { children: JSX.Element }) => (
Expand Down Expand Up @@ -58,26 +73,8 @@ describe('SetupRealAccountOrGoToDemoModalContent', () => {
it('User click "setup real account" button when is_cr_demo = true', () => {
(useContentFlag as jest.Mock).mockReturnValue({ is_cr_demo: true, is_eu_demo: false });

const mockedOpenRealAccountSignup = jest.fn();
const mockedSetIsFromSignupAccount = jest.fn();
const mockedSetIsSetupRealAccountOrGoToDemoModalVisible = jest.fn();

const mock = mockStore({
ui: {
is_from_signup_account: true,
openRealAccountSignup: mockedOpenRealAccountSignup,
setIsFromSignupAccount: mockedSetIsFromSignupAccount,
},
traders_hub: {
is_setup_real_account_or_go_to_demo_modal_visible: true,
setIsSetupRealAccountOrGoToDemoModalVisible: mockedSetIsSetupRealAccountOrGoToDemoModalVisible,
},
});

const mockTrack = Analytics.trackEvent;

const { container } = render(<SetupRealAccountOrGoToDemoModalContent />, {
wrapper: wrapper(mock),
wrapper: wrapper(),
});

const setup_btn = screen.getByRole('button', {
Expand All @@ -103,26 +100,8 @@ describe('SetupRealAccountOrGoToDemoModalContent', () => {
it('User click "setup real account" button when is_eu_demo = true', () => {
(useContentFlag as jest.Mock).mockReturnValue({ is_cr_demo: false, is_eu_demo: true });

const mockedOpenRealAccountSignup = jest.fn();
const mockedSetIsFromSignupAccount = jest.fn();
const mockedSetIsSetupRealAccountOrGoToDemoModalVisible = jest.fn();

const mock = mockStore({
ui: {
is_from_signup_account: true,
openRealAccountSignup: mockedOpenRealAccountSignup,
setIsFromSignupAccount: mockedSetIsFromSignupAccount,
},
traders_hub: {
is_setup_real_account_or_go_to_demo_modal_visible: true,
setIsSetupRealAccountOrGoToDemoModalVisible: mockedSetIsSetupRealAccountOrGoToDemoModalVisible,
},
});

const mockTrack = Analytics.trackEvent;

const { container } = render(<SetupRealAccountOrGoToDemoModalContent />, {
wrapper: wrapper(mock),
wrapper: wrapper(),
});

const setup_btn = screen.getByRole('button', {
Expand All @@ -146,24 +125,8 @@ describe('SetupRealAccountOrGoToDemoModalContent', () => {
});

it('User click "take me to demo" button', () => {
const mockedSetIsFromSignupAccount = jest.fn();
const mockedSetIsSetupRealAccountOrGoToDemoModalVisible = jest.fn();

const mock = mockStore({
ui: {
is_from_signup_account: true,
setIsFromSignupAccount: mockedSetIsFromSignupAccount,
},
traders_hub: {
is_setup_real_account_or_go_to_demo_modal_visible: true,
setIsSetupRealAccountOrGoToDemoModalVisible: mockedSetIsSetupRealAccountOrGoToDemoModalVisible,
},
});

const mockTrack = Analytics.trackEvent;

const { container } = render(<SetupRealAccountOrGoToDemoModalContent />, {
wrapper: wrapper(mock),
wrapper: wrapper(),
});

const demo_btn = screen.getByRole('button', {
Expand Down

0 comments on commit c8048df

Please sign in to comment.