Skip to content

Commit

Permalink
chore: #336 increase test coveraged no-redux option (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
dannd4 authored Feb 19, 2020
1 parent 885f828 commit ba311d8
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import * as React from 'react'
import { shallow } from 'enzyme'
import toJson from 'enzyme-to-json'
import { Login } from '../login'
import { AuthProvider } from '@/context/auth-context'
import Authenticated from '../authenticated'

describe('Login', () => {
describe('Authenticated', () => {
it('should match a snapshot', () => {
expect(
toJson(
shallow(
<AuthProvider>
<Login />
</AuthProvider>,
),
),
).toMatchSnapshot()
expect(toJson(shallow(<Authenticated />))).toMatchSnapshot()
})
})
Original file line number Diff line number Diff line change
@@ -1,19 +1,62 @@
import * as React from 'react'
import { shallow } from 'enzyme'
import toJson from 'enzyme-to-json'
import { Login } from '../login'
import { AuthProvider } from '@/context/auth-context'
import * as AuthContext from '@/context/auth-context'
import { redirectToLogin } from '@reapit/cognito-auth'

jest.mock('../../../context/auth-context.tsx')
jest.mock('@reapit/cognito-auth', () => ({
redirectToLogin: jest.fn(),
}))

const contextValues: AuthContext.AuthContext = {
logout: jest.fn(),
getLoginSession: jest.fn(),
setAuthState: jest.fn(),
fetching: false,
loginSession: {
userName: '1',
accessToken: '1',
accessTokenExpiry: 1000,
idToken: '1',
idTokenExpiry: 1000,
refreshToken: 'string',
loginType: 'DEVELOPER',
loginIdentity: {
email: '1',
name: '1',
developerId: null,
clientId: null,
adminId: null,
userCode: null,
},
mode: 'WEB',
cognitoClientId: '1',
},
}

describe('Login', () => {
it('should match a snapshot', () => {
expect(
toJson(
shallow(
<AuthProvider>
<Login />
</AuthProvider>,
),
),
).toMatchSnapshot()
jest.spyOn(AuthContext, 'useAuthContext').mockImplementation(() => ({ ...contextValues, loginSession: null }))

expect(shallow(<Login />)).toMatchSnapshot()
})

it('loginHandler should run correcly', () => {
jest.spyOn(AuthContext, 'useAuthContext').mockImplementation(() => ({ ...contextValues, loginSession: null }))

const wrapper = shallow(<Login />)

wrapper.find('[dataTest="login-button"]').simulate('click')

expect(redirectToLogin).toBeCalledWith(
process.env.COGNITO_CLIENT_ID_APP_NAME as string,
`${window.location.origin}`,
)
})

it('should match a snapshot with loginSession', () => {
jest.spyOn(AuthContext, 'useAuthContext').mockImplementation(() => contextValues)
expect(shallow(<Login />)).toMatchSnapshot()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { renderHook, act } from '@testing-library/react-hooks'
import { useAuth } from '../auth-context'
import { RefreshParams, removeSession, redirectToLogout } from '@reapit/cognito-auth'

jest.mock('@reapit/cognito-auth', () => ({
removeSession: jest.fn(),
getSession: jest.fn(),
redirectToLogout: jest.fn(),
}))

const refreshParams: RefreshParams = {
cognitoClientId: '1',
loginType: 'CLIENT',
mode: 'WEB',
redirectUri: '1',
authorizationCode: '1',
refreshToken: '1',
userName: '1',
state: null,
}

describe('auth-context', () => {
it('getLoginSession should run correctly', async () => {
const { result, waitForNextUpdate } = renderHook(() => useAuth())

act(() => {
result.current.getLoginSession(refreshParams)
})

expect(result.current.fetching).toBe(true)

await waitForNextUpdate()

expect(result.current.fetching).toBe(false)
expect(result.current.loginSession).toBe(undefined)
})

it('logout should run correctly', () => {
const { result } = renderHook(() => useAuth())

act(() => {
result.current.logout()
})

expect(removeSession).toHaveBeenCalled()
expect(redirectToLogout).toHaveBeenCalled()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as React from 'react'
import createContext from '../index'
import { mount, shallow } from 'enzyme'

function useCounter({ initialCount = 0 } = {}) {
const [count, setCount] = React.useState(initialCount)
const increment = React.useCallback(() => setCount(c => c + 1), [])
const decrement = () => setCount(count - 1)
return { count, increment, decrement }
}

describe('createContext', () => {
it('should run correctly', () => {
const [Provider, useContext] = createContext(useCounter)

const Increment = () => {
const { increment } = useContext()
return <button onClick={increment}>Increment</button>
}

const Count = () => {
const { count } = useContext()
return <div>{count}</div>
}

const App = () => (
<Provider initialCount={10}>
<Increment />
<Count />
</Provider>
)

const wrapper = mount(<App />)
expect(wrapper.find('div').text()).toEqual('10')
wrapper.find('button').simulate('click')
expect(wrapper.find('div').text()).toEqual('11')
})

it('without provider', async () => {
const [, useContext] = createContext(useCounter)
const App = () => {
const { count } = useContext()
return <div>{count}</div>
}
const spy = jest.spyOn(console, 'error').mockImplementation()
shallow(<App />)
expect(spy).toBeCalledWith('useContext must be inside a Provider with a value')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,41 @@ import * as React from 'react'
import { shallow } from 'enzyme'
import toJson from 'enzyme-to-json'
import { PrivateRouteWrapper, PrivateRouteWrapperProps } from '../private-route-wrapper'
import { AuthProvider } from '@/context/auth-context'
import * as AuthContext from '@/context/auth-context'

jest.mock('../../context/auth-context.tsx')
jest.mock('@reapit/cognito-auth', () => ({
redirectToLogin: jest.fn(),
getSessionCookie: jest.fn(),
getTokenFromQueryString: jest.fn(),
redirectToOAuth: jest.fn(),
}))

const contextValues: AuthContext.AuthContext = {
logout: jest.fn(),
getLoginSession: jest.fn(),
setAuthState: jest.fn(),
fetching: false,
loginSession: {
userName: '1',
accessToken: '1',
accessTokenExpiry: 1000,
idToken: '1',
idTokenExpiry: 1000,
refreshToken: 'string',
loginType: 'DEVELOPER',
loginIdentity: {
email: '1',
name: '1',
developerId: null,
clientId: null,
adminId: null,
userCode: null,
},
mode: 'WEB',
cognitoClientId: '1',
},
}

const props: PrivateRouteWrapperProps = {
path: '/',
Expand All @@ -12,17 +46,14 @@ const props: PrivateRouteWrapperProps = {
},
}


describe('PrivateRouter', () => {
it('should match a snapshot', () => {
expect(
toJson(
shallow(
<AuthProvider>
<PrivateRouteWrapper {...props} />
</AuthProvider>,
),
),
).toMatchSnapshot()
jest.spyOn(AuthContext, 'useAuthContext').mockImplementation(() => ({ ...contextValues, loginSession: null }))
expect(toJson(shallow(<PrivateRouteWrapper {...props} />))).toMatchSnapshot()
})

it('should match a snapshot with loginSession', () => {
jest.spyOn(AuthContext, 'useAuthContext').mockImplementation(() => ({ ...contextValues }))
expect(toJson(shallow(<PrivateRouteWrapper {...props} />))).toMatchSnapshot()
})
})

0 comments on commit ba311d8

Please sign in to comment.