1
0
mirror of https://github.com/kremalicious/portfolio.git synced 2025-01-03 18:35: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,6 +17,41 @@ import styles from './Layout.module.scss'
const timeout = 250 const timeout = 250
const RoutesContainer = posed.div(fadeIn) const RoutesContainer = posed.div(fadeIn)
const query = graphql`
query {
contentYaml {
allowedHosts
}
}
`
const LayoutMarkup = ({ children, isHomepage, allowedHosts }) => (
<>
<Typekit />
<HostnameCheck allowedHosts={allowedHosts} />
<PoseGroup animateOnMount={true}>
<RoutesContainer
key={location.pathname}
delay={timeout}
delayChildren={timeout}
>
<Header minimal={!isHomepage} />
<main className={styles.screen}>{children}</main>
</RoutesContainer>
</PoseGroup>
<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 { export default class Layout extends PureComponent {
static propTypes = { static propTypes = {
children: PropTypes.any.isRequired, children: PropTypes.any.isRequired,
@ -27,23 +63,22 @@ export default class Layout extends PureComponent {
const isHomepage = location.pathname === '/' const isHomepage = location.pathname === '/'
return ( return (
<> <StaticQuery
<Typekit /> query={query}
<HostnameCheck /> render={data => {
const { allowedHosts } = data.contentYaml
<PoseGroup animateOnMount={true}> return (
<RoutesContainer <LayoutMarkup
key={location.pathname} isHomepage={isHomepage}
delay={timeout} allowedHosts={allowedHosts}
delayChildren={timeout} location={location}
> >
<Header minimal={!isHomepage} /> {children}
<main className={styles.screen}>{children}</main> </LayoutMarkup>
</RoutesContainer> )
</PoseGroup> }}
/>
<Footer />
</>
) )
} }
} }

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()
})
})