Skip to content

Instantly share code, notes, and snippets.

@robneville73
Created May 17, 2019 13:54
Show Gist options
  • Save robneville73/d47ae0a02bc4d427df5015c6980054b5 to your computer and use it in GitHub Desktop.
Save robneville73/d47ae0a02bc4d427df5015c6980054b5 to your computer and use it in GitHub Desktop.
describe('router interceptor tests', () => {
describe('if unauthenticated, store to route and go to login route', () => {
// :shrug: I have no idea why you have to do it like this
// and only here??
store.getters = {
'auth/authenticated': false
};
const to = { name: 'blah' };
let nextObj = {
next: function () {
return null;
}
};
const nextSpy = jest.spyOn(nextObj, 'next');
const dispatchSpy = jest.spyOn(store, 'dispatch');
interceptor(to, {}, nextObj.next);
expect(nextSpy).toHaveBeenCalled();
expect(dispatchSpy).toBeCalledTimes(1);
expect(dispatchSpy.mock.calls[0]).toEqual([
"auth/rememberEntryRoute",
to
]);
expect(nextSpy.mock.calls[0]).toEqual([{ name: 'login' }]);
});
describe('if unauthenticated but going to login, just go on', () => {
store.getters = {
'auth/authenticated': false
};
const to2 = { name: 'login' };
let nextObj = {
next: function () {
return null;
}
};
// const nextSpy = jest.spyOn(nextObj, 'next');
const dispatchSpy2 = jest.spyOn(store, 'dispatch');
interceptor(to2, {}, nextObj.next);
expect(dispatchSpy2).toBeCalledTimes(0);
});
// describe('if authenticated, just go on', () => {
// null;
// });
});
@robneville73
Copy link
Author

putting console logs in the interceptor function shows that to == { name: 'blah' } in the second test... if I comment out test 1, it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment