Skip to content

Commit

Permalink
Add unit tests on client side
Browse files Browse the repository at this point in the history
  • Loading branch information
jbuget committed Jun 26, 2017
1 parent 5b7978f commit 63cbfab
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 40 deletions.
7 changes: 4 additions & 3 deletions client/src/components/LoginPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
Sign in with Google
</g-signin-button>

<div class="g-signin2" ref="g-signin2" data-onsuccess="onSignIn"></div>

</div>

</template>

<script>
import Vue from 'vue';
import GSignInButton from 'vue-google-signin-button';
import authentication from '@/services/authentication';
Vue.use(GSignInButton);
export default {
data() {
Expand All @@ -38,7 +40,6 @@
},
onSignInError(error) {
console.error(error);
},
},
Expand Down
3 changes: 0 additions & 3 deletions client/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import VueAnalytics from 'vue-analytics';
import GSignInButton from 'vue-google-signin-button';
import App from './App';
import router from './router';

Expand All @@ -13,8 +12,6 @@ Vue.use(VueAnalytics, {
router,
});

Vue.use(GSignInButton);

/* eslint-disable no-new */
new Vue({
el: '#app',
Expand Down
15 changes: 15 additions & 0 deletions client/test/unit/specs/api/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,20 @@ describe('Unit | API | auth api', () => {
expect(axios.post).to.have.been.calledWith(expectedUrl, expectedBody, expectedOptions);
});
});

it('should return a rejected promise when an error is thrown', (done) => {
// given
const idToken = 'invalid-id_token';
axios.post.rejects(new Error('some error'));

// when
const promise = api.verifyIdTokenAndGetAccessToken(idToken);

// then
promise.catch((error) => {
expect(error.message).to.equal('some error');
done();
});
});
});
});
72 changes: 43 additions & 29 deletions client/test/unit/specs/api/interests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@ import api from '@/api/interests';

describe('Unit | API | interests api', () => {
describe('#sendInterest', () => {
const job = {
id: 2,
activity: {
title: 'Tech Lead',
},
project: {
id: 123456,
status: 'proposal-in-progress',
name: 'SCLOU - Cloud computing : enjeux, architecture et gouvernance du IaaS, CaaS, PaaS INTER 2017',
customer: {
name: 'La Poste - Courrier',
},
start_date: 'juillet 2017',
duration: '10 mois',
location: 'OCTO',
business_contact: {
nickname: 'ABC',
},
mission_director: {
nickname: 'XYZ',
},
},
};
const consultant = {
name: 'Samurai Jack',
email: 'sjack@octo.com',
};
const accessToken = 'access-token';

beforeEach(() => {
const stubbedResponse = {
status: 200,
Expand All @@ -19,12 +48,6 @@ describe('Unit | API | interests api', () => {

it('should post interests to API with the good params', () => {
// given
const consultant = {
name: 'Samurai Jack',
email: 'sjack@octo.com',
};
const accessToken = 'valid-access-token';

const expectedUrl = 'http://localhost:3000/api/interests';
const expectedBody = {
interestedConsultant: {
Expand All @@ -38,29 +61,6 @@ describe('Unit | API | interests api', () => {
missionName: 'SCLOU - Cloud computing : enjeux, architecture et gouvernance du IaaS, CaaS, PaaS INTER 2017',
};
const expectedOptions = { headers: { Authorization: `Bearer ${accessToken}` } };
const job = {
id: 2,
activity: {
title: 'Tech Lead',
},
project: {
id: 123456,
status: 'proposal-in-progress',
name: 'SCLOU - Cloud computing : enjeux, architecture et gouvernance du IaaS, CaaS, PaaS INTER 2017',
customer: {
name: 'La Poste - Courrier',
},
start_date: 'juillet 2017',
duration: '10 mois',
location: 'OCTO',
business_contact: {
nickname: 'ABC',
},
mission_director: {
nickname: 'XYZ',
},
},
};

// when
const promise = api.sendInterest(job, consultant, accessToken);
Expand All @@ -70,5 +70,19 @@ describe('Unit | API | interests api', () => {
expect(axios.post).to.have.been.calledWith(expectedUrl, expectedBody, expectedOptions);
});
});

it('should return a rejected promise when an error is thrown', (done) => {
// given
axios.post.rejects(new Error('some error'));

// when
const promise = api.sendInterest(job, consultant, accessToken);

// then
promise.catch((error) => {
expect(error.message).to.equal('some error');
done();
});
});
});
});
15 changes: 15 additions & 0 deletions client/test/unit/specs/api/jobs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,20 @@ describe('Unit | API | jobs api', () => {
expect(axios.get).to.have.been.calledWith(expectedUrl, expectedOptions);
});
});

it('should return a rejected promise when an error is thrown', (done) => {
// given
const accessToken = 'invalid-access_token';
axios.get.rejects(new Error('some error'));

// when
const promise = api.fetchAll(accessToken);

// then
promise.catch((error) => {
expect(error.message).to.equal('some error');
done();
});
});
});
});
40 changes: 40 additions & 0 deletions client/test/unit/specs/components/AppHeader.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Vue from 'vue';
import AppHeader from '@/components/AppHeader';
import authenticationService from '@/services/authentication';

describe('Unit | Component | AppHeader.vue', () => {
let component;

beforeEach(() => {
// given
const Constructor = Vue.extend(AppHeader);

// when
component = new Constructor().$mount();
});

describe('rendering', () => {
it('should display a link to home', () => {
expect(component.$el.querySelector('.logo-link')).to.exist;
})
});

describe('#signOut', () => {
beforeEach(() => {
sinon.stub(authenticationService, 'disconnect').resolves();
});

afterEach(() => {
authenticationService.disconnect.restore();
});

it('should call the API with good params', () => {
// when
component.signOut();

// then
expect(authenticationService.disconnect).to.have.been.called;
});
});
});

24 changes: 21 additions & 3 deletions client/test/unit/specs/components/LoginPage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@ describe('Unit | Component | LoginPage.vue', () => {
let component;

beforeEach(() => {
window.gapi = {
load() {

},
auth2: {
getAuthInstance() {
return {};
},
},
};
const Constructor = Vue.extend(LoginPage);

component = new Constructor().$mount();
});

afterEach(() => {
delete window.gapi;
});

describe.skip('method #onSignIn', () => {
describe('method #onSignInSuccess', () => {
beforeEach(() => {
sinon.stub(authentication, 'authenticate').resolves();
window.localStorage.clear();
Expand All @@ -27,10 +39,16 @@ describe('Unit | Component | LoginPage.vue', () => {
const googleUser = {};

// when
component.onSignIn(googleUser);
component.onSignInSuccess(googleUser);

// then
expect(authentication.authenticate).to.have.been.called;
});
});

describe('method #onSignInError', () => {
it('should exist', () => {
expect(component.onSignInError).to.exist.and.to.be.a.function;
});
});
});
15 changes: 13 additions & 2 deletions client/test/unit/specs/services/authentication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ describe('Unit | Services | Auth', () => {
});

it('should execute Google Sign-in API #signOut method', () => {
// given

// when
const promise = authentication.disconnect();

Expand All @@ -175,6 +173,19 @@ describe('Unit | Services | Auth', () => {
expect(auth2.signOut).to.have.been.called;
});
});

it('should return a resolved promise even when window.gapi.auth2 is not loaded', (done) => {
// given
delete window.gapi;

// when
const promise = authentication.disconnect();

// then
promise.then(() => {
done();
});
});
});

describe('method #isAuthenticated', () => {
Expand Down

0 comments on commit 63cbfab

Please sign in to comment.