Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing with auth0 #2

Merged
merged 3 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
"rules": {
"no-underscore-dangle": "off",
"react/jsx-props-no-spreading": "off"
}
},
"overrides": [
{
"files": ["*.stories.js"],
"rules": { "import/no-extraneous-dependencies": "off" }
}
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@storybook/node-logger": "^6.1.16",
"@storybook/preset-create-react-app": "^3.1.5",
"@storybook/react": "^6.1.16",
"husky": "^4.3.8",
"husky": "3.1.0",
"plop": "^2.7.4"
},
"husky": {
Expand Down
6 changes: 3 additions & 3 deletions plop-templates/component.js.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React from 'react'
import PropTypes from 'prop-types'

/**
* Describe your component
*/
* Describe your component
*/
const {{pascalCase name}} = () => {
return <>{{pascalCase name}}</>
}

{{pascalCase name}}.propTypes = {}

export default {{ pascalCase name }}
export default {{ pascalCase name }}
10 changes: 4 additions & 6 deletions plop-templates/componentSpec.js.hbs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import '@testing-library/jest-dom/extend-expect'
import React from 'react'
import { render } from '@testing-library/react'

import { renderAuthenticated } from '../../testUtilities/testingLibraryWithHelpers'

import {{pascalCase name}} from './{{pascalCase name}}'

test('{{pascalCase name}} component renders with the expected UI elements', () => {
const utilities = render(
<{{pascalCase name}} />)
const utilities = renderAuthenticated(<{{pascalCase name}} />)

expect(utilities.getByText('I should fail'))
})
expect(utilities.getByText('I should fail'))
})
28 changes: 20 additions & 8 deletions src/App.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
// import React from 'react'
// import { render, screen } from '@testing-library/react'
// import App from './App'

test('renders learn react link', () => {
// render(<App />)
// const linkElement = screen.getByText(/learn react/i)
// expect(linkElement).toBeInTheDocument()
import React from 'react'
import {
renderAuthenticated,
renderUnauthenticated,
screen,
} from './testUtilities/testingLibraryWithHelpers'
import App from './App'

test('App renders the initial screen as expected for a authenticated user', () => {
renderAuthenticated(<App />)

// this assertion is for demo purposes only and will be deleted as the landing pages are built
expect(screen.getByText('All projects page placeholder'))
})

test('App renders the initial screen as expected for a not authenticated user', () => {
renderUnauthenticated(<App />)

// this assertion is for demo purposes only and will be deleted as the landing pages are built
expect(screen.queryByText('All projects page placeholder')).toBeNull()
})
4 changes: 2 additions & 2 deletions src/components/generic/ButtonMenu/ButtonMenu.stories.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react'
import { action } from '@storybook/addon-actions'
import React from 'react'
import styled from 'styled-components'

import ButtonMenu from '.'
import styled from 'styled-components'

export default {
title: 'ButtonMenu',
Expand Down
43 changes: 43 additions & 0 deletions src/testUtilities/testingLibraryWithHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Auth0Context } from '@auth0/auth0-react'
import { render } from '@testing-library/react'
import { ThemeProvider } from 'styled-components/macro'
import PropTypes from 'prop-types'
import React from 'react'
import theme from '../theme'

const AuthenticatedProviders = ({ children }) => (
<Auth0Context.Provider
value={{ isAuthenticated: true, user: { name: 'Fake User' } }}
>
<ThemeProvider theme={theme}>{children}</ThemeProvider>
</Auth0Context.Provider>
)

const UnauthenticatedProviders = ({ children }) => (
<Auth0Context.Provider
value={{ isAuthenticated: false, loginWithRedirect: () => {} }}
>
<ThemeProvider theme={theme}>{children}</ThemeProvider>
</Auth0Context.Provider>
)

AuthenticatedProviders.propTypes = {
children: PropTypes.node.isRequired,
}
UnauthenticatedProviders.propTypes = {
children: PropTypes.node.isRequired,
}

const renderAuthenticated = (ui, options) =>
render(ui, { wrapper: AuthenticatedProviders, ...options })
const renderUnauthenticated = (ui, options) =>
render(ui, { wrapper: UnauthenticatedProviders, ...options })

const renderOverride = () => {
throw new Error(
'Please use renderAuthenticated or renderUnauthenticated instead of render.',
)
}

export * from '@testing-library/react'
export { renderOverride as render, renderAuthenticated, renderUnauthenticated }
Loading