Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

george / add tests for verification_store in cashier package #5201

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions packages/cashier/src/Stores/__tests__/verification-store.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import VerificationStore from '../verification-store';

let root_store, verification_store, WS;

beforeEach(() => {
WS = {
verifyEmail: jest.fn(),
};
root_store = {
client: {
email: 'george@gmail.com',
setVerificationCode: jest.fn(),
},
modules: {
cashier: {
general_store: {
active_container: 'payment_agent',
},
payment_agent: jest.fn(),
},
},
};
verification_store = new VerificationStore({ WS, root_store });
});

describe('VerificationStore', () => {
it('should change value of the variable is_button_clicked', () => {
verification_store.setIsButtonClicked(false);

expect(verification_store.is_button_clicked).toBeFalse();

verification_store.setIsButtonClicked(true);

expect(verification_store.is_button_clicked).toBeTrue();
});
it('should change value of the variable timeout_button', () => {
verification_store.setTimeoutButton('100');

expect(verification_store.timeout_button).toBe('100');
});
it('should change value of the variable is_email_sent', () => {
verification_store.setIsEmailSent(false);

expect(verification_store.is_email_sent).toBeFalse();

verification_store.setIsEmailSent(true);

expect(verification_store.is_email_sent).toBeTrue();
});
it('should change value of the variable is_resend_clicked', () => {
verification_store.setIsResendClicked(false);

expect(verification_store.is_resend_clicked).toBeFalse();

verification_store.setIsResendClicked(true);

expect(verification_store.is_resend_clicked).toBeTrue();
});
it('should change value of the variable resend_timeout', () => {
verification_store.setResendTimeout('20');

expect(verification_store.resend_timeout).toBe('20');
});
it('should clear verification timeout', () => {
jest.useFakeTimers();
verification_store.setTimeoutButton('123');
verification_store.clearTimeoutVerification();

expect(clearTimeout).toHaveBeenCalledTimes(1);
expect(clearTimeout).toHaveBeenCalledWith('123');
jest.useRealTimers();
});
it('should set verification timeout', () => {
jest.useFakeTimers();
const spyClearTimeoutVerification = jest.spyOn(verification_store, 'clearTimeoutVerification');
const spyClearVerification = jest.spyOn(verification_store, 'clearVerification');
verification_store.setTimeoutVerification();
jest.runAllTimers();

expect(spyClearTimeoutVerification).toHaveBeenCalled();
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 3600000);
expect(spyClearVerification).toHaveBeenCalledTimes(1);
jest.useRealTimers();
});
it('should not send an email if is_button_clicked="true"', async () => {
verification_store.WS.verifyEmail.mockResolvedValue({});
verification_store.setIsButtonClicked(true);
await verification_store.sendVerificationEmail();

expect(verification_store.is_email_sent).toBeFalse();
});
it('should not send an email if there is no client email', async () => {
verification_store.root_store.client.email = '';
verification_store.WS.verifyEmail.mockResolvedValue({});
await verification_store.sendVerificationEmail();

expect(verification_store.is_email_sent).toBeFalse();
});
it('should send an email if there is no error in response_verify_email', async () => {
verification_store.WS.verifyEmail.mockResolvedValue({});
await verification_store.sendVerificationEmail();

expect(verification_store.is_email_sent).toBeTrue();
});
it('should set an error message when there is an error in response_verify_email with code="PaymentAgentWithdrawError"', async () => {
verification_store.WS.verifyEmail.mockResolvedValue({
error: { code: 'PaymentAgentWithdrawError', message: 'ERROR' },
});
await verification_store.sendVerificationEmail();

expect(verification_store.error.message).toBe('ERROR');
});
it('should set an error message when there is an error in response_verify_email with custom error code', async () => {
verification_store.WS.verifyEmail.mockResolvedValue({
error: { code: 'error_code', message: 'CUSTOM_ERROR' },
});
await verification_store.sendVerificationEmail();

expect(verification_store.error.message).toBe('CUSTOM_ERROR');
});
it('should resend verification email', () => {
const spySendVerificationEmail = jest.spyOn(verification_store, 'sendVerificationEmail');
verification_store.WS.verifyEmail.mockResolvedValue({});
verification_store.resendVerificationEmail();

expect(spySendVerificationEmail).toHaveBeenCalled();
});
it('should not resend verification email, if resend_timeout less then 60', () => {
const spySendVerificationEmail = jest.spyOn(verification_store, 'sendVerificationEmail');
verification_store.setResendTimeout(1);
verification_store.resendVerificationEmail();

expect(spySendVerificationEmail).not.toHaveBeenCalled();
});
it('should run clearInterval in setCountDownResendVerification function when resend_timeout === 1 ', () => {
jest.useFakeTimers();
verification_store.setCountDownResendVerification();
jest.runAllTimers();

expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 1000);
expect(verification_store.resend_timeout).toBe(60);
expect(clearInterval).toHaveBeenCalled();
jest.useRealTimers();
});
it('should clear verification', () => {
const spyClearTimeoutVerification = jest.spyOn(verification_store, 'clearTimeoutVerification');
const spySetResendTimeout = jest.spyOn(verification_store, 'setResendTimeout');
const spySetErrorMessage = jest.spyOn(verification_store.error, 'setErrorMessage');
verification_store.clearVerification();

expect(spyClearTimeoutVerification).toHaveBeenCalledTimes(1);
expect(verification_store.is_button_clicked).toBeFalse();
expect(verification_store.is_email_sent).toBeFalse();
expect(verification_store.is_resend_clicked).toBeFalse();
expect(spySetResendTimeout).toHaveBeenCalledWith(60);
expect(spySetErrorMessage).toHaveBeenCalledWith('', null, null);
expect(verification_store.root_store.client.setVerificationCode).toHaveBeenCalledWith(
'',
'payment_agent_withdraw'
);
});
});