-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
196 additions
and
34 deletions.
There are no files selected for viewing
15 changes: 3 additions & 12 deletions
15
...ct-app-scaffolder/app/templates/no-redux/src/components/pages/__tests__/authenticated.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
65 changes: 54 additions & 11 deletions
65
...ages/react-app-scaffolder/app/templates/no-redux/src/components/pages/__tests__/login.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
48 changes: 48 additions & 0 deletions
48
packages/react-app-scaffolder/app/templates/no-redux/src/context/__tests__/auth-context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
49 changes: 49 additions & 0 deletions
49
packages/react-app-scaffolder/app/templates/no-redux/src/context/__tests__/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters