Skip to content

Commit

Permalink
feat(core): ✨ add prevent custom + update README
Browse files Browse the repository at this point in the history
  • Loading branch information
thierrymichel committed Jan 28, 2019
1 parent f74f499 commit 2fb4ec6
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
collectCoverageFrom: [
'packages/**/src/**/*.{js,jsx}',
'!packages/core/src/(schema|utils).js',
'!packages/core/src/(polyfills|schema|utils).js',
],
coverageThreshold: {
global: {
Expand Down
18 changes: 18 additions & 0 deletions packages/core/__tests__/barba/barba.init.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import barba from '../../src';
import cache from '../../src/cache';
import history from '../../src/history';
import prevent from '../../src/prevent';

const wrapper = document.createElement('div');
const container = document.createElement('div');
Expand Down Expand Up @@ -35,6 +36,23 @@ it('needs barba container', () => {
expect(init).toThrow('No Barba container found');
});

it('needs valid prevent custom', () => {
const init = function init() {
barba.init({ prevent: 'bad' });
};

expect(init).toThrow('Prevent should be a function');
expect(prevent._tests).not.toHaveProperty('preventCustom');
});

it('adds prevent custom', () => {
document.body.appendChild(wrapper);
wrapper.appendChild(container);
barba.init({ prevent() {} }); // eslint-disable-line no-empty-function

expect(prevent._tests).toHaveProperty('preventCustom');
});

it('has DOM elements', () => {
document.body.appendChild(wrapper);
wrapper.appendChild(container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const el = document.createElement('a');
parent.appendChild(el);

beforeEach(() => {
check = jest.fn(data => prevent._tests.hasPreventAll(data));
check = jest.fn(data => prevent._tests.preventAll(data));
[...el.attributes].forEach(attr => el.removeAttribute(attr.name));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let check;
const el = document.createElement('a');

beforeEach(() => {
check = jest.fn(data => prevent._tests.hasPrevent(data));
check = jest.fn(data => prevent._tests.preventSelf(data));
[...el.attributes].forEach(attr => el.removeAttribute(attr.name));
});

Expand Down
4 changes: 0 additions & 4 deletions packages/core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ const jestBase = require('../../jest.config.js');

module.exports = {
...jestBase,
collectCoverageFrom: [
'<rootDir>/src/*.{js,jsx}',
'!<rootDir>/src/(schema|utils).js',
],
};
10 changes: 10 additions & 0 deletions packages/core/src/barba.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export default {
transitions = [],
views = [],
schema = attributeSchema,
prevent: preventCustom = null,
useCache = true,
usePrefetch = true,
} = {}) {
Expand All @@ -117,12 +118,21 @@ export default {
dom.init({ attributeSchema: schema });
// Init prevent with data-attributes schema
prevent.init({ attributeSchema: schema });
// Add prevent custom
if (preventCustom !== null) {
if (typeof preventCustom !== 'function') {
throw new Error('Prevent should be a function');
}

prevent.add('preventCustom', preventCustom);
}

// Get wrapper
this._wrapper = dom.getWrapper();
if (!this._wrapper) {
throw new Error('No Barba wrapper found');
}

// A11y
this._wrapper.setAttribute('aria-live', 'polite');

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/prevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ const prevent = {
// );

// If contains [data-barba-prevent] or [data-barba-prevent="self"]
this.add('hasPrevent', ({ el }) =>
this.add('preventSelf', ({ el }) =>
el.hasAttribute(`${this.attr.prefix}-${this.attr.prevent}`)
);

// If ancestor contains [data-barba-prevent="all"]
this.add('hasPreventAll', ({ el }) =>
this.add('preventAll', ({ el }) =>
Boolean(el.closest(`[${this.attr.prefix}-${this.attr.prevent}="all"]`))
);
},
Expand Down
1 change: 0 additions & 1 deletion packages/router/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ const jestBase = require('../../jest.config.js');

module.exports = {
...jestBase,
collectCoverageFrom: ['<rootDir>/src/*.{js,jsx}'],
};

0 comments on commit 2fb4ec6

Please sign in to comment.