Skip to content

Commit f61b2c8

Browse files
author
Anthony Du Pont
committed
🚧 Add Unit Test
1 parent 9f2affe commit f61b2c8

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

test/core.tests.js

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ import Home from './dom/home';
2323
// Update Document
2424
global.document = Home.page;
2525

26-
// Core instance
27-
const Core = new Highway.Core();
28-
2926
// Assertions
3027
describe('Highway.Core', () => {
3128
const a = document.createElement('a');
@@ -37,19 +34,20 @@ describe('Highway.Core', () => {
3734
document.body.appendChild(a);
3835
document.body.appendChild(b);
3936

40-
sinon.spy(a, 'click');
41-
sinon.spy(b, 'click');
42-
43-
sinon.spy(a, 'addEventListener');
44-
sinon.spy(b, 'removeEventListener');
45-
4637
before(() => fetchMock.get('/foo', 'bar'));
4738

4839
it('Should be an instance of `Highway.Core`', () => {
40+
const Core = new Highway.Core();
41+
4942
expect(Core).to.be.instanceof(Highway.Core);
5043
});
5144

5245
it('Should bind/unbind `click` event on links', () => {
46+
const Core = new Highway.Core();
47+
48+
sinon.spy(a, 'addEventListener');
49+
sinon.spy(b, 'removeEventListener');
50+
5351
Core.bind();
5452
Core.unbind();
5553

@@ -58,6 +56,9 @@ describe('Highway.Core', () => {
5856
});
5957

6058
it('Should call `click` method on `click` event', () => {
59+
sinon.spy(a, 'click');
60+
sinon.spy(b, 'click');
61+
6162
a.click();
6263
b.click();
6364

@@ -66,6 +67,8 @@ describe('Highway.Core', () => {
6667
});
6768

6869
it('Should call `pushState` method on `click` event', () => {
70+
const Core = new Highway.Core();
71+
6972
Core.pushState = sinon.spy();
7073
Core.state = { pathname: '' };
7174

@@ -75,6 +78,8 @@ describe('Highway.Core', () => {
7578
});
7679

7780
it('Should call `beforeFetch` method on `popState`', () => {
81+
const Core = new Highway.Core();
82+
7883
Core.beforeFetch = sinon.spy();
7984

8085
Core.state = { pathname: '' };
@@ -83,14 +88,34 @@ describe('Highway.Core', () => {
8388
expect(Core.beforeFetch.calledOnce).to.be.true;
8489
});
8590

86-
it('Should fetch an URL properly', () => {
87-
Core.state = { url: '/foo' };
91+
it('Should call `beforeFetch` method on `pushState`', () => {
92+
const Core = new Highway.Core();
8893

89-
Core.fetch().then((response) => {
90-
expect(response).to.equal('bar');
91-
expect(fetchMock).route('/foo').to.have.been.called;
92-
});
94+
Core.beforeFetch = sinon.spy();
95+
Core.pushState({ target: { href: '/foo' }});
96+
97+
expect(Core.beforeFetch.calledOnce).to.be.true;
9398
});
9499

100+
it('Should call `unbind` method on `beforeFetch`', () => {
101+
const Core = new Highway.Core();
102+
103+
Core.unbind = sinon.spy();
104+
Core.beforeFetch();
105+
106+
expect(Core.unbind.calledOnce).to.be.true;
107+
});
108+
109+
// it('Should fetch an URL properly', () => {
110+
// const Core = new Highway.Core();
111+
112+
// Core.state = { url: '/foo' };
113+
114+
// Core.fetch().then((response) => {
115+
// expect(response).to.equal('bar');
116+
// expect(fetchMock).route('/foo').to.have.been.called;
117+
// });
118+
// });
119+
95120
after(() => fetchMock.restore());
96121
});

0 commit comments

Comments
 (0)