Initial testing with jest (#133)

* 🎉 initial testing with jest

* 👌 update test script names & remove package-lock.json

* 👌 add 'yarn test' step to circle-ci build workflow
This commit is contained in:
Davyd McColl
2019-10-14 20:48:46 +02:00
committed by Sojan Jose
parent cdada37e3a
commit 01b72ca051
10 changed files with 1051 additions and 46 deletions

View File

@@ -0,0 +1,11 @@
import App from './App';
import '../../test-matchers';
describe(`App component`, () => {
it(`should be a component`, () => {
// Arrange
// Act
expect(App).toBeVueComponent('App');
// Assert
});
});

View File

@@ -40,7 +40,7 @@ const generateRoleWiseRoute = route => {
// returns an object with roles as keys and routeArr as values
generateRoleWiseRoute(routes);
const router = new VueRouter({
export const router = new VueRouter({
mode: 'history',
routes, // short for routes: routes
});
@@ -84,7 +84,7 @@ const routeValidators = [
},
];
const validateAuthenticateRoutePermission = (to, from, next) => {
export const validateAuthenticateRoutePermission = (to, from, next) => {
const isLoggedIn = auth.isLoggedIn();
const isProtectedRoute = !unProtectedRoutes.includes(to.name);
const strategy = routeValidators.find(

View File

@@ -0,0 +1,112 @@
import 'expect-more-jest';
import { validateAuthenticateRoutePermission } from './index';
import auth from '../api/auth';
jest.mock('./dashboard/dashboard.routes', () => ({
routes: [],
}));
jest.mock('./auth/auth.routes', () => ({
routes: [],
}));
jest.mock('./login/login.routes', () => ({
routes: [],
}));
jest.mock('../constants', () => {
return {
APP_BASE_URL: '/',
PUSHER: false,
get apiUrl() {
return `${this.APP_BASE_URL}/`;
},
GRAVATAR_URL: 'https://www.gravatar.com/avatar',
CHANNELS: {
FACEBOOK: 'facebook',
},
ASSIGNEE_TYPE_SLUG: {
MINE: 0,
UNASSIGNED: 1,
OPEN: 1,
},
};
});
window.roleWiseRoutes = {};
describe(`behavior`, () => {
describe(`when route is not protected`, () => {
it(`should go to the dashboard when user is logged in`, () => {
// Arrange
spyOn(auth, 'isLoggedIn').and.returnValue(true);
spyOn(auth, 'getCurrentUser').and.returnValue({
role: 'user',
});
const to = {
name: 'login',
};
const from = { name: '' };
const next = jest.fn();
// Act
validateAuthenticateRoutePermission(to, from, next);
// Assert
expect(next).toHaveBeenCalledWith('/app/dashboard');
});
});
describe(`when route is protected`, () => {
describe(`when user not logged in`, () => {
it(`should redirect to login`, () => {
// Arrange
spyOn(auth, 'isLoggedIn').and.returnValue(false);
spyOn(auth, 'getCurrentUser').and.returnValue(null);
const to = {
name: 'some-protected-route',
};
const from = { name: '' };
const next = jest.fn();
// Act
validateAuthenticateRoutePermission(to, from, next);
// Assert
expect(next).toHaveBeenCalledWith('/app/login');
});
});
describe(`when user is logged in`, () => {
describe(`when route is not accessible to current user`, () => {
it(`should redirect to dashboard`, () => {
// Arrange
spyOn(auth, 'isLoggedIn').and.returnValue(true);
spyOn(auth, 'getCurrentUser').and.returnValue({
role: 'user',
});
window.roleWiseRoutes.user = ['dashboard'];
const to = {
name: 'admin',
};
const from = { name: '' };
const next = jest.fn();
// Act
validateAuthenticateRoutePermission(to, from, next);
// Assert
expect(next).toHaveBeenCalledWith('/app/dashboard');
});
});
describe(`when route is accessible to current user`, () => {
it(`should go there`, () => {
// Arrange
spyOn(auth, 'isLoggedIn').and.returnValue(true);
spyOn(auth, 'getCurrentUser').and.returnValue({
role: 'user',
});
window.roleWiseRoutes.user = ['dashboard', 'admin'];
const to = {
name: 'admin',
};
const from = { name: '' };
const next = jest.fn();
// Act
validateAuthenticateRoutePermission(to, from, next);
// Assert
expect(next).toHaveBeenCalledWith();
});
});
});
});
});