Skip to content

Commit

Permalink
fix(core): 🐛 fix sameUrl + anchors
Browse files Browse the repository at this point in the history
Closes #359
  • Loading branch information
thierrymichel committed Mar 24, 2019
1 parent 3520fab commit 039f5d9
Show file tree
Hide file tree
Showing 18 changed files with 298 additions and 178 deletions.
8 changes: 4 additions & 4 deletions packages/core/__tests__/core/core.click.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import barba from '../../src';
const { link, span, click } = init();

// Mocks
barba.go = jest.fn();
barba.page = jest.fn();

it('handle link enter', () => {
link.href = 'foo';
span.dispatchEvent(click);

expect(barba.go).toHaveBeenCalledTimes(1);
expect(barba.page).toHaveBeenCalledTimes(1);
});

it('handle link enter with same url', () => {
link.href = 'http://localhost/';
span.dispatchEvent(click);

expect(barba.go).toHaveBeenCalledTimes(0);
expect(barba.page).toHaveBeenCalledTimes(0);
});

it('handle link enter with prevent', () => {
link.href = 'foo';
link.dataset.barbaPrevent = '';
span.dispatchEvent(click);

expect(barba.go).toHaveBeenCalledTimes(0);
expect(barba.page).toHaveBeenCalledTimes(0);
});
134 changes: 15 additions & 119 deletions packages/core/__tests__/core/core.go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,9 @@ import { schemaAttribute } from '../../src/schemas/attribute';
(global as any).Headers = class {};

const namespace = 'test';
const checkDoc = new RegExp(
`^<html>[\\s\\S]+body[\\s\\S]+${schemaAttribute.wrapper}[\\s\\S]+${
schemaAttribute.container
}[\\s\\S]+${namespace}[\\s\\S]+</html>$`
);
const t = { leave() {}, enter() {} };

init();

let spyCacheHas: jest.SpyInstance;
let spyCacheGet: jest.SpyInstance;
let spyCacheSet: jest.SpyInstance;
let spyPage: jest.SpyInstance;

beforeEach(() => {
barba.init();
self.fetch = jest.fn().mockImplementation(() => ({
Expand All @@ -35,129 +24,36 @@ beforeEach(() => {
</body>
</html>`),
}));
spyCacheHas = jest.spyOn(barba.cache, 'has');
spyCacheGet = jest.spyOn(barba.cache, 'get');
spyCacheSet = jest.spyOn(barba.cache, 'set');
spyPage = jest.spyOn(barba.transitions, 'doPage');
});
afterEach(() => {
spyCacheHas.mockRestore();
spyCacheGet.mockRestore();
spyCacheSet.mockRestore();
spyPage.mockRestore();
barba.destroy();
});

it('do go', async () => {
// barba.transitions.doPage = jest.fn();
barba.history.push = jest.fn();
hooks.do = jest.fn();
hooks.before = jest.fn();
hooks.after = jest.fn();

barba.transitions.store.add('transition', t);
const data = {
...barba.data,
trigger: 'barba',
};
barba.page = jest.fn();

await barba.go('http://localhost/foo');

expect(spyCacheHas).toHaveBeenCalledTimes(1);
expect(spyCacheSet).toHaveBeenCalledTimes(1);
expect(barba.history.push).toHaveBeenCalledTimes(1);
expect(hooks.do).toHaveBeenNthCalledWith(1, 'go', data);
expect(hooks.do).toHaveBeenNthCalledWith(3, 'beforeLeave', data, t);
expect(hooks.do).toHaveBeenNthCalledWith(7, 'beforeEnter', data, t);
// expect(barba.transitions.doPage).toHaveBeenCalledTimes(1);
});

it('do go [popstate]', () => {
barba.transitions.doPage = jest.fn();
barba.history.add = jest.fn();

barba.transitions.store.add('transition', { leave() {}, enter() {} });
barba.go('http://localhost/foo', 'popstate');

expect(barba.history.add).toHaveBeenCalledTimes(1);
});

it('do go [has cache]', () => {
barba.transitions.doPage = jest.fn();
barba.history.add = jest.fn();

barba.transitions.store.add('transition', { leave() {}, enter() {} });
barba.go('http://localhost/');

expect(spyCacheHas).toHaveBeenCalledTimes(1);
expect(spyCacheGet).toHaveBeenCalledTimes(1);
expect(spyCacheSet).toHaveBeenCalledTimes(0);
expect(barba.page).toHaveBeenCalledWith(
'http://localhost/foo',
'barba',
false
);
});

it('do go [waiting]', async () => {
barba.transitions.doPage = jest.fn();
// Avoid updating data.next
barba['_resetData'] = jest.fn();
it('prevent same url with no self transition', async () => {
barba.page = jest.fn();

barba.transitions.store.add('transition', {
leave() {},
enter() {},
to: { namespace: 'ns' },
});
await barba.go('http://localhost');
await barba.go('http://localhost/');

expect(barba.data.next.html).toMatch(checkDoc);
expect(barba.page).not.toHaveBeenCalled();
});

// Now, Cache (extends Ignore) manages this…
// it('do go [no use cache]', () => {
// barba.cacheIgnore = true;

// barba.transitions.doPage = jest.fn();
// barba.history.add = jest.fn();

// barba.transitions.store.add('transition', { leave() {}, enter() {} });
// barba.go('http://localhost');

// expect(spyCacheHas).toHaveBeenCalledTimes(0);
// expect(spyCacheGet).toHaveBeenCalledTimes(0);
// expect(spyCacheSet).toHaveBeenCalledTimes(0);
// });

it('force when manager running', () => {
barba.force = jest.fn();
barba.transitions.doPage = jest.fn();
hooks.do = jest.fn();

barba.transitions.store.add('transition', { leave() {}, enter() {} });
barba.transitions['_running'] = true;
barba.go('http://localhost/foo');

expect(barba.force).toHaveBeenCalledTimes(1);
expect(hooks.do).not.toHaveBeenCalled();
expect(barba.transitions.doPage).not.toHaveBeenCalled();

barba.transitions['_running'] = false;
});

it('catches error', async () => {
expect.assertions(3);
barba.logger.error = jest.fn();
barba.transitions.logger.error = jest.fn();
barba.history.cancel = jest.fn();
spyPage.mockRestore();
const errorLeave = new Error('Transition error [page][leave]');
const errorTransition = new Error('Transition error');

barba.transitions.store.add('transition', {
leave() {
throw errorLeave;
},
});
it('use self transition on same url', async () => {
barba.page = jest.fn();
barba.transitions.store.add('transition', { name: 'self' });

await barba.go('http://localhost');
await barba.go('http://localhost/');

expect(barba.transitions.logger.error).toHaveBeenCalledWith(errorLeave);
expect(barba.logger.error).toHaveBeenCalledWith(errorTransition);
expect(barba.history.cancel).toHaveBeenCalledTimes(1);
expect(barba.page).toHaveBeenCalledWith('http://localhost/', 'barba', true);
});
147 changes: 147 additions & 0 deletions packages/core/__tests__/core/core.page.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/* tslint:disable:no-empty no-string-literal */
import { init } from '../../__mocks__/barba';
import barba from '../../src';
import { hooks } from '../../src/hooks';
import { schemaAttribute } from '../../src/schemas/attribute';

// Needed for "request" module
(global as any).Headers = class {};

const namespace = 'test';
const checkDoc = new RegExp(
`^<html>[\\s\\S]+body[\\s\\S]+${schemaAttribute.wrapper}[\\s\\S]+${
schemaAttribute.container
}[\\s\\S]+${namespace}[\\s\\S]+</html>$`
);
const t = { leave() {}, enter() {} };

init();

let spyCacheHas: jest.SpyInstance;
let spyCacheGet: jest.SpyInstance;
let spyCacheSet: jest.SpyInstance;
let spyPage: jest.SpyInstance;

beforeEach(() => {
barba.init();
self.fetch = jest.fn().mockImplementation(() => ({
status: 200,
text: () =>
Promise.resolve(`<html>
<body>
<div data-barba="wrapper">
<div data-barba="container" data-barba-namespace="${namespace}"></div>
</div>
</body>
</html>`),
}));
spyCacheHas = jest.spyOn(barba.cache, 'has');
spyCacheGet = jest.spyOn(barba.cache, 'get');
spyCacheSet = jest.spyOn(barba.cache, 'set');
spyPage = jest.spyOn(barba.transitions, 'doPage');
});
afterEach(() => {
spyCacheHas.mockRestore();
spyCacheGet.mockRestore();
spyCacheSet.mockRestore();
spyPage.mockRestore();
barba.destroy();
});

it('do page', async () => {
barba.history.push = jest.fn();
hooks.do = jest.fn();

barba.transitions.store.add('transition', t);
const data = {
...barba.data,
trigger: 'barba',
};

await barba.page('http://localhost/foo', 'barba');

expect(spyCacheHas).toHaveBeenCalledTimes(1);
expect(spyCacheSet).toHaveBeenCalledTimes(1);
expect(barba.history.push).toHaveBeenCalledTimes(1);
expect(hooks.do).toHaveBeenNthCalledWith(1, 'page', data);
expect(hooks.do).toHaveBeenNthCalledWith(2, 'before', data, t);
expect(hooks.do).toHaveBeenNthCalledWith(3, 'beforeLeave', data, t);
expect(hooks.do).toHaveBeenNthCalledWith(7, 'beforeEnter', data, t);
expect(barba.transitions.doPage).toHaveBeenCalledTimes(1);
});

it('do page [popstate]', async () => {
barba.transitions.doPage = jest.fn();
barba.history.add = jest.fn();

barba.transitions.store.add('transition', { leave() {}, enter() {} });
await barba.page('http://localhost/foo', 'popstate');

expect(barba.history.add).toHaveBeenCalledTimes(1);
});

it('do page [has cache]', async () => {
// barba.transitions.doPage = jest.fn();
barba.history.add = jest.fn();

// NOTE: as we use "same URL" (localhost), we need a "self" transition
// to avoid prevent "sameURL"
barba.transitions.store.add('transition', { name: 'self' });
barba.transitions.store.add('transition', { leave() {}, enter() {} });
await barba.page('http://localhost/', 'barba');

expect(spyCacheHas).toHaveBeenCalledTimes(1);
expect(spyCacheGet).toHaveBeenCalledTimes(1);
expect(spyCacheSet).toHaveBeenCalledTimes(0);
});

it('do page [waiting]', async () => {
// Avoid updating data.next
barba['_resetData'] = jest.fn();

barba.transitions.store.add('transition', {
leave() {},
enter() {},
to: { namespace: 'ns' },
});
await barba.page('http://localhost', 'barba');

expect(barba.data.next.html).toMatch(checkDoc);
});

it('force when manager running', async () => {
barba.force = jest.fn();
hooks.do = jest.fn();

barba.transitions.store.add('transition', { leave() {}, enter() {} });
barba.transitions['_running'] = true;
await barba.page('http://localhost/foo', 'barba');

expect(barba.force).toHaveBeenCalledTimes(1);
expect(hooks.do).not.toHaveBeenCalled();
expect(barba.transitions.doPage).not.toHaveBeenCalled();

barba.transitions['_running'] = false;
});

it('catches error', async () => {
expect.assertions(3);
barba.logger.error = jest.fn();
barba.transitions.logger.error = jest.fn();
barba.history.cancel = jest.fn();
spyPage.mockRestore();
const errorLeave = new Error('Transition error [page][leave]');
const errorTransition = new Error('Transition error');

barba.transitions.store.add('transition', {
leave() {
throw errorLeave;
},
});

await barba.page('http://localhost', 'barba');

expect(barba.transitions.logger.error).toHaveBeenCalledWith(errorLeave);
expect(barba.logger.error).toHaveBeenCalledWith(errorTransition);
expect(barba.history.cancel).toHaveBeenCalledTimes(1);
});
13 changes: 5 additions & 8 deletions packages/core/__tests__/modules/store/store.log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ it('has debug info', () => {
Logger.setLevel('info');

store.logger.info = jest.fn();
store.resolve(
{
current: schemaPage,
next: schemaPage,
trigger: 'barba',
},
false
);
store.resolve({
current: schemaPage,
next: schemaPage,
trigger: 'barba',
});

expect(store.logger.info).toHaveBeenCalledTimes(1);
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ it('get "appear" transition', () => {
({
current: { namespace: 'none' },
} as unknown) as ITransitionData,
true
{ appear: true }
);

expect(result).toBe(tAppear);
Expand All @@ -31,7 +31,7 @@ it('get "appear/ns" transition', () => {
({
current: { namespace: 'ns' },
} as unknown) as ITransitionData,
true
{ appear: true }
);

expect(result).toBe(tAppearNs);
Expand All @@ -42,7 +42,7 @@ it('get "appear/custom" transition', () => {
({
current: { namespace: 'custom' },
} as unknown) as ITransitionData,
true
{ appear: true }
);

expect(result).toBe(tAppearCustom);
Expand Down
Loading

0 comments on commit 039f5d9

Please sign in to comment.