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

Unit Test : Add User Function Test #864

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
61 changes: 61 additions & 0 deletions apps/signup/signup.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

const { addUser } = require('./signup');
const fetchMock = require('fetch-mock');


global.window.alert = jest.fn();

describe('addUser function tests', () => {
beforeEach(() => {

fetchMock.restore();
});

it('should alert if email is invalid', () => {
document.getElementById = jest.fn().mockReturnValue({ value: 'invalid_email' });
addUser();
expect(window.alert).toHaveBeenCalledWith('Please enter a valid email');
});

it('should send correct data for admin user type', async () => {
document.getElementById = jest.fn()
.mockReturnValueOnce({ value: '[email protected]' })
.mockReturnValueOnce({ value: 'filters' })
.mockReturnValueOnce({ selectedIndex: 2 });

fetchMock.post('../../data/User/post', 200);

await addUser();

expect(fetchMock.lastOptions('../../data/User/post').body).toEqual(JSON.stringify({
email: '[email protected]',
userType: 'Admin',
userFilter: 'filters'
}));
expect(window.alert).toHaveBeenCalledWith('User registered successfully');
});

it('should send correct data for non-admin user type with permission', async () => {
document.getElementById = jest.fn()
.mockReturnValueOnce({ value: '[email protected]' })
.mockReturnValueOnce({ value: 'filters' })
.mockReturnValueOnce({ selectedIndex: 2 });


global.store = {
getUserPermissions: jest.fn().mockResolvedValue({ user: { post: true } })
};

fetchMock.post('../../data/User/post', 200);

await addUser();

expect(fetchMock.lastOptions('../../data/User/post').body).toEqual(JSON.stringify({
email: '[email protected]',
userType: 'Editor',
userFilter: 'filters'
}));
expect(window.alert).toHaveBeenCalledWith('User registered successfully');
});

});
Loading