From 66cc85e6ce9fe5a20284cf11909d8967f9219269 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Sun, 8 Dec 2019 10:47:56 +0100 Subject: [PATCH] Fix failing tests by passing Router through render --- test/index.js | 122 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 96 insertions(+), 26 deletions(-) diff --git a/test/index.js b/test/index.js index a63492be..842eecb5 100644 --- a/test/index.js +++ b/test/index.js @@ -1,5 +1,5 @@ import { Router, Link, route } from 'src'; -import { h } from 'preact'; +import { h, render } from 'preact'; import assertCloneOf from '../test_helpers/assert-clone-of'; chai.use(assertCloneOf); @@ -12,14 +12,35 @@ describe('preact-router', () => { }); describe('Router', () => { + let scratch; + let router; + + beforeEach(() => { + scratch = document.createElement('div'); + document.body.appendChild(scratch); + }); + + afterEach(() => { + document.body.removeChild(scratch); + router.componentWillUnmount(); + }); + it('should filter children based on URL', () => { - let router = new Router({}); let children = [ , , ]; + render( + ( + (router = ref)}> + {children} + + ), + scratch + ); + expect( router.render({ children }, { url:'/foo' }) ).to.be.cloneOf(children[1]); @@ -34,13 +55,22 @@ describe('preact-router', () => { }); it('should support nested parameterized routes', () => { - let router = new Router({}); let children = [ , , ]; + render( + ( + (router = ref)}> + {children} + + ), + scratch + ); + + expect( router.render({ children }, { url:'/foo' }) ).to.be.cloneOf(children[0]); @@ -55,13 +85,21 @@ describe('preact-router', () => { }); it('should support default routes', () => { - let router = new Router({}); let children = [ , , ]; + render( + ( + (router = ref)}> + {children} + + ), + scratch + ); + expect( router.render({ children }, { url:'/foo' }) ).to.be.cloneOf(children[2]); @@ -76,32 +114,59 @@ describe('preact-router', () => { }); it('should support initial route prop', () => { - let router = new Router({ url:'/foo' }); let children = [ , , ]; + render( + ( + (router = ref)}> + {children} + + ), + scratch + ); + expect( router.render({ children }, router.state) ).to.be.cloneOf(children[2]); - expect(new Router({})).to.have.deep.property('state.url', location.pathname + (location.search || '')); + render(null, scratch); + + render( + ( + (router = ref)}> + {children} + + ), + scratch + ); + + expect(router).to.have.deep.property('state.url', location.pathname + (location.search || '')); }); it('should support custom history', () => { let push = sinon.spy(); let replace = sinon.spy(); + let listen = sinon.spy(); let getCurrentLocation = sinon.spy(() => ({pathname: '/initial'})); - let router = new Router({ - history: { push, replace, getCurrentLocation }, - children: [ - , - , - - ] - }); + + let children = [ + , + , + + ]; + + render( + ( + (router = ref)}> + {children} + + ), + scratch + ); router.componentWillMount(); @@ -121,23 +186,28 @@ describe('preact-router', () => { describe('route()', () => { let router; - - before( () => { - router = new Router({ - url: '/foo', - children: [ - , - - ] - }, {}); + let scratch; + + beforeEach(() => { + scratch = document.createElement('div'); + document.body.appendChild(scratch); + + render( + ( + (router = ref)}> + + + + ), + scratch + ); sinon.spy(router, 'routeTo'); - - router.componentWillMount(); }); - after( () => { + afterEach(() => { router.componentWillUnmount(); + document.body.removeChild(scratch); }); it('should return true for existing route', () => {