1
0
mirror of https://github.com/kremalicious/portfolio.git synced 2025-01-05 11:25:00 +01:00

add some tests

This commit is contained in:
Matthias Kretschmann 2019-04-14 16:58:20 +02:00
parent 7f3a94d921
commit 09a119f78c
Signed by: m
GPG Key ID: 606EEEF3C479A91F
5 changed files with 90 additions and 24 deletions

View File

@ -27,3 +27,8 @@ typekitID: dtg3zui
# Analytics tools # Analytics tools
matomoUrl: https://analytics.kremalicious.com matomoUrl: https://analytics.kremalicious.com
matomoSite: 2 matomoSite: 2
allowedHosts:
- matthiaskretschmann.com
- beta.matthiaskretschmann.com
- localhost

View File

@ -35,6 +35,11 @@
"addressbook": "/matthias-kretschmann.vcf", "addressbook": "/matthias-kretschmann.vcf",
"typekitID": "dtg3zui", "typekitID": "dtg3zui",
"matomoUrl": "https://analytics.kremalicious.com", "matomoUrl": "https://analytics.kremalicious.com",
"matomoSite": 2 "matomoSite": 2,
"allowedHosts": [
"matthiaskretschmann.com",
"beta.matthiaskretschmann.com",
"localhost"
]
} }
} }

View File

@ -1,6 +1,7 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import posed, { PoseGroup } from 'react-pose' import posed, { PoseGroup } from 'react-pose'
import { StaticQuery, graphql } from 'gatsby'
import { fadeIn } from './atoms/Transitions' import { fadeIn } from './atoms/Transitions'
import Typekit from './atoms/Typekit' import Typekit from './atoms/Typekit'
import HostnameCheck from './atoms/HostnameCheck' import HostnameCheck from './atoms/HostnameCheck'
@ -16,20 +17,18 @@ import styles from './Layout.module.scss'
const timeout = 250 const timeout = 250
const RoutesContainer = posed.div(fadeIn) const RoutesContainer = posed.div(fadeIn)
export default class Layout extends PureComponent { const query = graphql`
static propTypes = { query {
children: PropTypes.any.isRequired, contentYaml {
location: PropTypes.object.isRequired allowedHosts
} }
}
`
render() { const LayoutMarkup = ({ children, isHomepage, allowedHosts }) => (
const { children, location } = this.props
const isHomepage = location.pathname === '/'
return (
<> <>
<Typekit /> <Typekit />
<HostnameCheck /> <HostnameCheck allowedHosts={allowedHosts} />
<PoseGroup animateOnMount={true}> <PoseGroup animateOnMount={true}>
<RoutesContainer <RoutesContainer
@ -45,5 +44,41 @@ export default class Layout extends PureComponent {
<Footer /> <Footer />
</> </>
) )
LayoutMarkup.propTypes = {
children: PropTypes.any.isRequired,
isHomepage: PropTypes.bool.isRequired,
allowedHosts: PropTypes.array.isRequired,
location: PropTypes.object.isRequired
}
export default class Layout extends PureComponent {
static propTypes = {
children: PropTypes.any.isRequired,
location: PropTypes.object.isRequired
}
render() {
const { children, location } = this.props
const isHomepage = location.pathname === '/'
return (
<StaticQuery
query={query}
render={data => {
const { allowedHosts } = data.contentYaml
return (
<LayoutMarkup
isHomepage={isHomepage}
allowedHosts={allowedHosts}
location={location}
>
{children}
</LayoutMarkup>
)
}}
/>
)
} }
} }

View File

@ -1,17 +1,16 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import Helmet from 'react-helmet' import Helmet from 'react-helmet'
import styles from './HostnameCheck.module.scss' import styles from './HostnameCheck.module.scss'
const allowedHosts = [ export default class HostnameCheck extends PureComponent {
'matthiaskretschmann.com', static propTypes = {
'beta.matthiaskretschmann.com', allowedHosts: PropTypes.array.isRequired
'localhost' }
]
export default class HostnameInfo extends PureComponent {
checkAllowedHost = () => { checkAllowedHost = () => {
if (typeof window !== 'undefined' && window.location) { if (typeof window !== 'undefined' && window.location) {
return allowedHosts.includes(window.location.hostname) return this.props.allowedHosts.includes(window.location.hostname)
} }
} }

View File

@ -0,0 +1,22 @@
import React from 'react'
import { render } from 'react-testing-library'
import HostnameCheck from './HostnameCheck'
describe('HostnameCheck', () => {
it('can access window.location', () => {
expect(window).not.toBe(undefined)
expect(window.location).not.toBe(undefined)
})
it('renders correctly', () => {
const allowedHosts = ['hello.com']
const { container } = render(<HostnameCheck allowedHosts={allowedHosts} />)
expect(container.firstChild).toHaveTextContent('do a remix')
})
it('does not render if on correct hostname', () => {
const allowedHosts = ['localhost']
const { container } = render(<HostnameCheck allowedHosts={allowedHosts} />)
expect(container.firstChild).not.toBeInTheDocument()
})
})