Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
richardzcode committed Jun 8, 2018
1 parent 34e5c2b commit 0d2d779
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 4 deletions.
14 changes: 13 additions & 1 deletion packages/aws-amplify-react/__tests__/Auth/ConfirmSignUp-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ describe('ConfirmSignIn', () => {
}
});

test('render correctly with hide', () => {
const wrapper = shallow(<ConfirmSignUp/>);
for (var i = 0; i < acceptedStates.length; i += 1){
wrapper.setProps({
authState: acceptedStates[i],
theme: AmplifyTheme,
hide: [ConfirmSignUp]
});
expect(wrapper).toMatchSnapshot();
}
});

test('simulate clicking confirm button with username already defined in auth data', async () => {
const spyon = jest.spyOn(Auth, 'confirmSignUp')
.mockImplementation((user, code) => {
Expand Down Expand Up @@ -131,4 +143,4 @@ describe('ConfirmSignIn', () => {
}
});
});
})
})
14 changes: 13 additions & 1 deletion packages/aws-amplify-react/__tests__/Auth/Greetings-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ describe('Greetings', () => {
}
});

test('render correctly with hide', () => {
const wrapper = shallow(<Greetings/>);
for (var i = 0; i < acceptedStates.length; i += 1){
wrapper.setProps({
authState: acceptedStates[i],
theme: 'theme',
hide: [Greetings]
});
expect(wrapper).toMatchSnapshot();
}
});

test('render name from attributes', () => {
const wrapper = shallow(<Greetings/>);
wrapper.setProps({
Expand Down Expand Up @@ -227,4 +239,4 @@ describe('Greetings', () => {
});

});
});
});
15 changes: 14 additions & 1 deletion packages/aws-amplify-react/__tests__/Auth/SignIn-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ describe('SignIn', () => {
}
});

test('render correctly with hide', () => {
for (var i = 0; i < acceptedStates.length; i += 1){
const wrapper = shallow(<SignIn/>);
wrapper.setProps({
authState: acceptedStates[i],
theme: AmplifyTheme,
hide: [SignIn]
});

expect(wrapper).toMatchSnapshot();
}
});

test('when clicking signIn and new password required', async () => {
const wrapper = shallow(<SignIn/>);
wrapper.setProps({
Expand Down Expand Up @@ -266,4 +279,4 @@ describe('SignIn', () => {
spyon2.mockClear();
})
});
})
})
279 changes: 279 additions & 0 deletions packages/aws-amplify-react/__tests__/Auth/SignOut-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
/*
jest.mock('aws-sdk-mobile-analytics', () => {
const Manager = () => {}
Manager.prototype.recordEvent = () => {
}
Manager.prototype.recordMonetizationEvent = () => {
}
var ret = {
Manager: Manager
}
return ret;
});
jest.mock('aws-sdk/clients/pinpoint', () => {
const Pinpoint = () => {
var pinpoint = null;
return pinpoint;
}
Pinpoint.prototype.updateEndpoint = (params, callback) => {
callback(null, 'data');
}
return Pinpoint;
});
*/

import SignOut from '../../src/Auth/SignOut';
import React from 'react';
import AmplifyTheme from '../../src/AmplifyTheme';
import AuthPiece from '../../src/Auth/AuthPiece';
import { Header, Footer, InputRow, ButtonRow } from '../../src/AmplifyUI';
import { Auth } from 'aws-amplify';

const acceptedStates = [
'signedIn'
];

const deniedStates = [
'signIn',
'signedUp',
'signedOut',
'forgotPassword',
'signUp',
'confirmSignIn',
'confirmSignUp',
'verifyContact'
];

describe('SignOut', () => {
describe('normal case', () => {
test('render correctly with authState signedIn', () => {
const wrapper = shallow(<SignOut/>);
for (var i = 0; i < acceptedStates.length; i += 1){
wrapper.setProps({
authState: acceptedStates[i],
theme: 'theme'
});
expect(wrapper).toMatchSnapshot();
}
});

test('render correctly with hide', () => {
const wrapper = shallow(<SignOut/>);
for (var i = 0; i < acceptedStates.length; i += 1){
wrapper.setProps({
authState: acceptedStates[i],
theme: 'theme',
hide: [SignOut]
});
expect(wrapper).toMatchSnapshot();
}
});

test('render correctly with empty hide', () => {
const wrapper = shallow(<SignOut/>);
for (var i = 0; i < acceptedStates.length; i += 1){
wrapper.setProps({
authState: acceptedStates[i],
theme: 'theme',
hide: []
});
expect(wrapper).toMatchSnapshot();
}
});

test('render name from attributes', () => {
const wrapper = shallow(<SignOut/>);
wrapper.setProps({
authState: 'signedIn',
theme: 'theme'
});

wrapper.setState({
authData: {
attributes: {
name: 'name'
}
},
authState: 'signedIn'
})

expect(wrapper).toMatchSnapshot();
})
});


test('render correctly with other authStates', () => {
const wrapper = shallow(<SignOut/>);

for (var i = 0; i < deniedStates.length; i += 1){
wrapper.setProps({
authState: deniedStates[i],
theme: 'theme'
});

expect(wrapper).toMatchSnapshot();
}
});

describe('signOut test', () => {
test('happy case', async () => {
const wrapper = shallow(<SignOut/>);
const signOut = wrapper.instance();

const spyon = jest.spyOn(Auth, 'signOut').mockImplementationOnce(() => {
return Promise.resolve();
});

await signOut.signOut();

expect(spyon).toBeCalled();
spyon.mockClear();
});

test('error case', async () => {
const wrapper = shallow(<SignOut/>);
const signOut = wrapper.instance();

const spyon = jest.spyOn(Auth, 'signOut').mockImplementationOnce(() => {
return Promise.reject('error');
});

await signOut.signOut();

expect(spyon).toBeCalled();
spyon.mockClear();
});
});

describe('google signOut test', () => {
test('happy case', async () => {
const mockFn = jest.fn();

window.gapi = {
auth2: {
getAuthInstance() {
return Promise.resolve({
signOut: mockFn
})
}
}
};

const wrapper = shallow(<SignOut/>);
const signOut = wrapper.instance();

await signOut.googleSignOut();

expect(mockFn).toBeCalled();
});

test('no auth2', async () => {
window.gapi = null;
const wrapper = shallow(<SignOut/>);
const signOut = wrapper.instance();

expect(await signOut.googleSignOut()).toBeNull();
});

test('no googleAuth', async () => {
window.gapi = {
auth2: {
getAuthInstance() {
return Promise.resolve(null);
}
}
};

const wrapper = shallow(<SignOut/>);
const signOut = wrapper.instance();

await signOut.googleSignOut();
});
});

describe('facebook signout test', () => {
test('happy case', async () => {
window.FB = {
getLoginStatus(callback) {
callback({
status: 'connected'
})
},
logout(callback) {
callback('response');
}
}

const wrapper = shallow(<SignOut/>);
const signOut = wrapper.instance();

await signOut.facebookSignOut()
});

test('not connected', async () => {
window.FB = {
getLoginStatus(callback) {
callback({
status: 'not connected'
})
},
logout(callback) {
callback('response');
}
}
const wrapper = shallow(<SignOut/>);
const signOut = wrapper.instance();

await signOut.facebookSignOut()
});
});

describe('checkUser test', () => {
test('happy case', async () => {
const wrapper = shallow(<SignOut/>);
const signOut = wrapper.instance();

const spyon = jest.spyOn(Auth, 'currentAuthenticatedUser').mockImplementationOnce(() => {
return Promise.resolve('user');
})

await signOut.checkUser();

expect(spyon).toBeCalled();
});

test('no user case', async () => {
const wrapper = shallow(<SignOut/>);
const signOut = wrapper.instance();

const spyon = jest.spyOn(Auth, 'currentAuthenticatedUser').mockImplementationOnce(() => {
return Promise.reject('no user');
})

await signOut.checkUser();

expect(spyon).toBeCalled();
});

test('check user on mount', async () => {
const wrapper = shallow(<SignOut/>);
const signOut = wrapper.instance();

const spyon = jest.spyOn(Auth, 'currentAuthenticatedUser').mockImplementationOnce(() => {
return Promise.resolve('user');
})

await signOut.componentDidMount();

expect(spyon).toBeCalled();
});
});
});
13 changes: 12 additions & 1 deletion packages/aws-amplify-react/__tests__/Auth/SignUp-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ describe('signUp', () => {
}
});

test('render correctly with hide', () => {
for (var i = 0; i < acceptedStates.length; i += 1){
wrapper.setProps({
authState: acceptedStates[i],
theme: AmplifyTheme,
hide: [SignUp]
});
expect(wrapper).toMatchSnapshot();
}
});

test('when clicking signUp', async () => {
const wrapper = shallow(<SignUp/>);
wrapper.setProps({
Expand Down Expand Up @@ -108,4 +119,4 @@ describe('signUp', () => {
}
});
});
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2713,6 +2713,8 @@ exports[`ConfirmSignIn normal case render correctly with Props confirmSignUp 1`]
</FormSection>
`;

exports[`ConfirmSignIn normal case render correctly with hide 1`] = `""`;

exports[`ConfirmSignIn null case with other authState render corrently 1`] = `""`;

exports[`ConfirmSignIn null case with other authState render corrently 2`] = `""`;
Expand Down
Loading

0 comments on commit 0d2d779

Please sign in to comment.