Skip to content

Commit

Permalink
Merge pull request #7 from Palmabit-IT/develop
Browse files Browse the repository at this point in the history
Increase tests coverage
  • Loading branch information
Giuseppe Aremare committed Oct 5, 2018
2 parents 7a1eb2d + b5fbd2b commit 655d843
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/Routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ export default class Routes {

getRouter(Router) {
const wrap = method => (route, params, locale, options) => {
const { byName, urls: { as, href } } = this.findAndGetUrls(route, locale, params)
return Router[method](href, as, byName ? options : params)
const { urls: { as, href } } = this.findAndGetUrls(route, locale, params)
return Router[method](href, as, options)
}

Router.pushRoute = wrap('push')
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/getRegionalLocale.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

import locales from './../config/regionalLocales'
export default (locale = '') => {
export default (locale) => {

if (!locale) {
return ''
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/routeHelper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

export const generateRouteFromObjectName = (routeObject = {}, defaultLocale) => {
export const generateRouteFromObjectName = (routeObject, defaultLocale) => {
const options = routeObject

if (!options.name) {
if (!options || !options.name) {
throw new Error('Unnamed routes not supported')
}

Expand Down
4 changes: 2 additions & 2 deletions src/seo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export default Child => class extends Component {
Seo.defaultProps = Object.assign({}, Seo.defaultProps, { req: { nextRoute, getMultilanguageUrls, siteUrl, routeUrl } })
const childProps = await childInitialProps(ctx)

return { ...childProps, SeoComponent: Seo || Component }
return { ...childProps, SeoComponent: Seo }
}

render() {
const { SeoComponent = Component } = this.props
const { SeoComponent } = this.props
return (
<Child {...this.props} SeoComponent={SeoComponent} />
)
Expand Down
8 changes: 1 addition & 7 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ export const setupRouterMethods = (routerMethods, ...args) => {
})
}

const testException = (argos) => {
routerMethods.forEach(method => {
const Router = routes.getRouter({ [method]: jest.fn() })
expect(() => Router[`${method}Route`](...argos)).toThrow()
})
}
return { routes, route, testMethods, testException }
return { routes, route, testMethods }
}

export const setupRoute = (...args) => {
Expand Down
10 changes: 9 additions & 1 deletion test/helpers/routeHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('generateRouteFromObject()', () => {
it('return object with defined structure', () => {
const objectRoute = { name: 'bar', page: 'page', locale: 'en', update: true, foo: 'bar' }

const result = generateRouteFromObjectName(objectRoute);
const result = generateRouteFromObjectName(objectRoute)

expect(result).toHaveProperty('name')
expect(result).toHaveProperty('page')
Expand All @@ -23,6 +23,14 @@ describe('generateRouteFromObject()', () => {
expect(result).not.toHaveProperty('foo')
})

it('should return an object with default locale if objectRoute is missing locale', () => {
const objectRoute = { name: 'bar', page: 'page', update: true, foo: 'bar' }
const defaultLocale = 'it'

const { locale } = generateRouteFromObjectName(objectRoute, defaultLocale)
expect(locale).toBe(defaultLocale)
})

it('return detected locale', () => {
const req = {
acceptsLanguages: () => { return ['it-IT', 'it', 'en', 'es'] }
Expand Down
20 changes: 14 additions & 6 deletions test/middleware/middleware.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import MiddlewareManager from "../../src/middleware/MiddlewareManager";
import MiddlewareManager from '../../src/middleware/MiddlewareManager'

/* global jest, describe, test, expect */

Expand All @@ -21,7 +21,7 @@ describe('run sequence', () => {
cb(null, 'foo')
})
const fn2 = jest.fn((data, cb) => {
cb(null, data + '_bar')
cb(null, `${data}_bar`)
})

MiddlewareManager([fn1, fn2], {})((err, data) => {
Expand All @@ -34,17 +34,25 @@ describe('run sequence', () => {
test('can block execution if at least a middleware thrown an error ', () => {

const fn1 = jest.fn((data, cb) => {
cb(new Error("this is an error"))
cb(new Error('this is an error'))
})
const fn2 = jest.fn((data, cb) => {
cb(null, data + '_bar')
cb(null, `${data}_bar`)
})

MiddlewareManager([fn1, fn2], {})((err, data) => {
MiddlewareManager([fn1, fn2], {})(err => {
expect(fn1.mock.calls.length).toBe(1)
expect(fn2.mock.calls.length).toBe(0)
expect(err).toBeInstanceOf(Error)
expect(err.message).toBe("this is an error")
expect(err.message).toBe('this is an error')
})
})

test('should handle calls with no arguments', () => {

MiddlewareManager()((err, data) => {
expect(data).toBeTruthy()
expect(err).toBe(null)
})
})
})
24 changes: 24 additions & 0 deletions test/routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,28 @@ describe('middleware()', () => {
expect(res.statusCode).toBe(418)

})

test('should set error code to 500 if error has no statusCode', () => {
const testFunct = (params, cb) => {
const error = new Error('this is an error')
cb(error)
}

const routes = nextRoutes({ locale: 'it' })
routes.add('a', 'en', '/').middleware([testFunct])

const app = {
getRequestHandler: () => { },
renderError: jest.fn()
}
const requestHandler = routes.getRequestHandler(app)
const req = { url: '/en' }
const res = {}

requestHandler(req, res)

expect(app.renderError.mock.calls.length).toBe(1)
expect(res.statusCode).toBe(500)

})
})
9 changes: 9 additions & 0 deletions test/seo/HOCSeo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ describe('High Order Component', () => {
const result = shallow(<FakeComponent {...props} />)
expect(result.html()).toContain('<meta property="og:title" content="foo"/>')
})

test('should render a component with expected props', async () => {
routes.getRequestHandler({ getRequestHandler: jest.fn(), render: jest.fn() })(req)
const ComponentWrapped = withSeo(FakeComponent)
const props = await ComponentWrapped.getInitialProps({})
const { props: renderedProps } = new ComponentWrapped(props).render()
expect(renderedProps).toHaveProperty('foo')
expect(renderedProps.foo).toBe('bar')
})
})
13 changes: 11 additions & 2 deletions test/seo/SeoComponent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ describe('Component: Seo', () => {

})

test('getOgLocale should return null if called with falsy locale', () => {
const seoComponent = shallow(<Seo description={'Lorem ipsum dolor sit amet'} />)

const result = seoComponent.instance().getOgLocale({ nextRoute: { locale: undefined } })

expect(result).toBeNull()

})

test('can return component with locale field', () => {
const routeData = { name: 'home', locale: 'it', page: 'home', pattern: '/', forceLocale: true }
const activeRoute = new Route(routeData)
Expand Down Expand Up @@ -105,7 +114,7 @@ describe('Component: Seo', () => {


test('can not return hreflang if lang is once', () => {
const routes = nextRoutes({ locale: 'it', siteUrl: 'https://test.com' })
routes = nextRoutes({ locale: 'it', siteUrl: 'https://test.com' })
.add('home', 'it', '/', 'homepage')
.add('newsDetail', 'en', '/:slug', 'newsDetail')
.add('newsDetail', 'it', '/:slug', 'newsDetail')
Expand All @@ -126,7 +135,7 @@ describe('Component: Seo', () => {
})

test('can return component with hreflang', () => {
const routes = nextRoutes({ locale: 'it', siteUrl: 'https://test.com' })
routes = nextRoutes({ locale: 'it', siteUrl: 'https://test.com' })
.add('newsDetail', 'en', '/:slug', 'newsDetail')
.add('newsDetail', 'it', '/:slug', 'newsDetail')
.add('newsDetail', 'de', '/:slug', 'newsDetail')
Expand Down

0 comments on commit 655d843

Please sign in to comment.