Skip to content

Commit

Permalink
fix: #641 - First time log in and terms accepted cookies not setting …
Browse files Browse the repository at this point in the history
…correctly (#694)
  • Loading branch information
NghiaPham authored Mar 23, 2020
1 parent 52b0955 commit 58578a1
Show file tree
Hide file tree
Showing 26 changed files with 574 additions and 338 deletions.
44 changes: 26 additions & 18 deletions packages/marketplace/src/actions/__tests__/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import {
authChangeLoginType,
authSetRefreshSession,
authClear,
checkFirstTimeLogin,
toggleFirstLogin,
userAcceptTermAndCondition,
setInitDeveloperTermsAcceptedStateFromCookie,
setDeveloperTermAcceptedCookieAndState,
setInitClientTermsAcceptedStateFromCookie,
setClientTermAcceptedCookieAndState,
} from '../auth'
import ActionTypes from '../../constants/action-types'
import { LoginType, LoginSession, LoginMode } from '@reapit/cognito-auth'
Expand All @@ -31,6 +32,28 @@ describe('auth actions', () => {
expect(authLoginSuccess(loginSession).data).toEqual(loginSession)
})

it('should create a setInitDeveloperTermsAcceptedStateFromCookie action', () => {
expect(setInitDeveloperTermsAcceptedStateFromCookie.type).toEqual(
ActionTypes.SET_INIT_DEVELOPER_TERMS_ACCEPTED_STATE_FROM_COOKIE,
)
})

it('should create a setDeveloperTermAcceptedCookieAndState action', () => {
expect(setDeveloperTermAcceptedCookieAndState.type).toEqual(
ActionTypes.SET_DEVELOPER_TERM_ACCEPTED_COOKIE_AND_STATE,
)
})

it('should create a setInitClientTermsAcceptedStateFromCookie action', () => {
expect(setInitClientTermsAcceptedStateFromCookie.type).toEqual(
ActionTypes.SET_INIT_CLIENT_TERMS_ACCEPTED_STATE_FROM_COOKIE,
)
})

it('should create a setClientTermAcceptedCookieAndState action', () => {
expect(setClientTermAcceptedCookieAndState.type).toEqual(ActionTypes.SET_CLIENT_TERM_ACCEPTED_COOKIE_AND_STATE)
})

it('should create a authLoginFailure action', () => {
expect(authLoginFailure.type).toEqual(ActionTypes.AUTH_LOGIN_FAILURE)
})
Expand Down Expand Up @@ -67,19 +90,4 @@ describe('auth actions', () => {
expect(authClear.type).toEqual(ActionTypes.AUTH_CLEAR)
expect(authClear().data).toEqual(undefined)
})

it('should create a checkFirstTimeLogin action', () => {
expect(checkFirstTimeLogin.type).toEqual(ActionTypes.CHECK_FIRST_TIME_LOGIN)
expect(checkFirstTimeLogin().data).toBeUndefined()
})

it('should create a toggleFirstLogin action', () => {
expect(toggleFirstLogin.type).toEqual(ActionTypes.TOGGLE_FIRST_LOGIN)
expect(toggleFirstLogin(true).data).toBeTruthy()
})

it('should create a userAcceptTermAndCondition action', () => {
expect(userAcceptTermAndCondition.type).toEqual(ActionTypes.USER_ACCEPT_TERM_AND_CONDITION)
expect(userAcceptTermAndCondition().data).toBeUndefined()
})
})
21 changes: 16 additions & 5 deletions packages/marketplace/src/actions/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ export const authLogoutSuccess = actionCreator<void>(ActionTypes.AUTH_LOGOUT_SUC
export const authSetRefreshSession = actionCreator<RefreshParams>(ActionTypes.AUTH_SET_REFRESH_SESSION)
export const authChangeLoginType = actionCreator<LoginType>(ActionTypes.AUTH_CHANGE_LOGIN_TYPE)
export const authClear = actionCreator<void>(ActionTypes.AUTH_CLEAR)
export const checkFirstTimeLogin = actionCreator<void>(ActionTypes.CHECK_FIRST_TIME_LOGIN)
export const toggleFirstLogin = actionCreator<boolean>(ActionTypes.TOGGLE_FIRST_LOGIN)
export const userAcceptTermAndCondition = actionCreator<void>(ActionTypes.USER_ACCEPT_TERM_AND_CONDITION)
export const checkTermsAcceptedWithCookie = actionCreator<void>(ActionTypes.CHECK_TERM_ACCEPTED_WITH_COOKIE)
export const setTermsAcceptedWithCookie = actionCreator<boolean>(ActionTypes.SET_TERMS_ACCEPTED_WITH_COOKIE)

export const setInitDeveloperTermsAcceptedStateFromCookie = actionCreator<void>(
ActionTypes.SET_INIT_DEVELOPER_TERMS_ACCEPTED_STATE_FROM_COOKIE,
)
export const setDeveloperTermAcceptedCookieAndState = actionCreator<boolean>(
ActionTypes.SET_DEVELOPER_TERM_ACCEPTED_COOKIE_AND_STATE,
)

export const setInitClientTermsAcceptedStateFromCookie = actionCreator<void>(
ActionTypes.SET_INIT_CLIENT_TERMS_ACCEPTED_STATE_FROM_COOKIE,
)

export const setClientTermAcceptedCookieAndState = actionCreator<boolean>(
ActionTypes.SET_CLIENT_TERM_ACCEPTED_COOKIE_AND_STATE,
)

export const setTermsAcceptedState = actionCreator<boolean>(ActionTypes.SET_TERMS_ACCEPTED_STATE)
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,7 @@ exports[`ClientWelcomeMessage should match a snapshot 1`] = `
}
heading="Lorem Ipsum"
id="step-5"
render={
<Unknown
onAccept={[MockFunction]}
/>
}
render={<Unknown />}
subHeading="What is Lorem Ipsum?"
/>
</Component>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ exports[`Client should match a snapshot when LOADING false 1`] = `
afterClose={[Function]}
visible={false}
/>
<Component
onAccept={[Function]}
visible={false}
/>
</Connect(ErrorBoundary)>
`;

Expand All @@ -100,10 +96,6 @@ exports[`Client should match a snapshot when LOADING true 1`] = `
afterClose={[Function]}
visible={false}
/>
<Component
onAccept={[Function]}
visible={false}
/>
</Connect(ErrorBoundary)>
`;

Expand Down Expand Up @@ -188,10 +180,6 @@ exports[`Client should match a snapshot when featured apps is empty [] 1`] = `
afterClose={[Function]}
visible={false}
/>
<Component
onAccept={[Function]}
visible={false}
/>
</Connect(ErrorBoundary)>
`;

Expand Down Expand Up @@ -276,9 +264,5 @@ exports[`Client should match a snapshot when featured apps is undefined 1`] = `
afterClose={[Function]}
visible={false}
/>
<Component
onAccept={[Function]}
visible={false}
/>
</Connect(ErrorBoundary)>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,7 @@ exports[`DeveloperWelcomeMessage should match a snapshot 1`] = `
}
heading="On going support"
id="step-5"
render={
<Unknown
onAccept={[MockFunction]}
/>
}
render={<Unknown />}
subHeading="We’re here to help."
/>
</Component>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
jest.mock('@/utils/cookie')

import * as React from 'react'
import { setCookieString, COOKIE_CLIENT_FIRST_TIME_LOGIN_COMPLETE, COOKIE_MAX_AGE_INFINITY } from '@/utils/cookie'
import MockDate from 'mockdate'
import { shallow } from 'enzyme'
import {
ClientWelcomeMessage,
ClientWelcomeMessageProps,
handleUserAccept,
mapDispatchToProps,
Support,
Welcome,
} from '../client-welcome'
import { ClientWelcomeMessage, ClientWelcomeMessageProps, handleUserAccept, Support, Welcome } from '../client-welcome'
import routes from '@/constants/routes'
import { HelpGuide } from '@reapit/elements'

Expand Down Expand Up @@ -51,26 +48,29 @@ describe('ClientWelcomeMessage', () => {
})

describe('handleUserAccept', () => {
it('should call dispatch', () => {
const mockUserAcceptTermAndCondition = jest.fn()
it('should set COOKIE_CLIENT_FIRST_TIME_LOGIN_COMPLETE cookie', () => {
const TIME_OFFSET = 0
MockDate.set('2019-12-18T16:30:00', TIME_OFFSET)

const mockHistory = {
push: jest.fn(),
}
const fn = handleUserAccept(mockUserAcceptTermAndCondition, mockHistory)
const fn = handleUserAccept(mockHistory)
fn()
expect(mockUserAcceptTermAndCondition).toBeCalled()
expect(mockHistory.push).toBeCalledWith(routes.INSTALLED_APPS)
})
})

describe('mapDispatchToProps', () => {
describe('login', () => {
it('should call dispatch correctly', () => {
const mockDispatch = jest.fn()
const { userAcceptTermAndCondition } = mapDispatchToProps(mockDispatch)
userAcceptTermAndCondition()
expect(mockDispatch).toBeCalled()
})
expect(setCookieString).toHaveBeenCalledWith(
COOKIE_CLIENT_FIRST_TIME_LOGIN_COMPLETE,
new Date(),
COOKIE_MAX_AGE_INFINITY,
)
})
it('should redirect to INSTALLED_APPS', () => {
const mockHistory = {
push: jest.fn(),
}
const fn = handleUserAccept(mockHistory)
fn()
expect(mockHistory.push).toBeCalledWith(routes.INSTALLED_APPS)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ const props = (loading: boolean): ClientProps => ({
installationsFormState: 'PENDING',
installationsSetFormState: jest.fn(),
fetchAppDetail: jest.fn(),
userAcceptTermAndCondition: jest.fn(),
firstLogin: false,
...routerProps,
})

Expand Down Expand Up @@ -105,8 +103,6 @@ describe('Client', () => {
setStateViewBrowse: jest.fn(),
installationsSetFormState: jest.fn(),
fetchAppDetail: jest.fn(),
userAcceptTermAndCondition: jest.fn(),
firstLogin: false,
...routerProps,
}

Expand Down Expand Up @@ -137,8 +133,6 @@ describe('Client', () => {
setStateViewBrowse: jest.fn(),
installationsSetFormState: jest.fn(),
fetchAppDetail: jest.fn(),
userAcceptTermAndCondition: jest.fn(),
firstLogin: false,
...routerProps,
}

Expand Down Expand Up @@ -246,7 +240,7 @@ describe('Client', () => {
const wrapper = mount(
<Provider store={store.reduxStore}>
<Router>
<Client {...props(false)} firstLogin />
<Client {...props(false)} />
</Router>
</Provider>,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as React from 'react'
import MockDate from 'mockdate'
import { shallow } from 'enzyme'
import {
DeveloperWelcomeMessage,
DeveloperWelcomeMessageProps,
handleUserAccept,
mapDispatchToProps,
Submitting,
Managing,
Support,
Expand All @@ -13,6 +13,9 @@ import {
import routes from '@/constants/routes'
import { HelpGuide } from '@reapit/elements'

jest.mock('@/utils/cookie')
import { setCookieString, COOKIE_DEVELOPER_FIRST_TIME_LOGIN_COMPLETE, COOKIE_MAX_AGE_INFINITY } from '@/utils/cookie'

const mockProps: DeveloperWelcomeMessageProps = {
userAcceptTermAndCondition: jest.fn(),
}
Expand Down Expand Up @@ -73,26 +76,29 @@ describe('DeveloperWelcomeMessage', () => {
})

describe('handleUserAccept', () => {
it('should call dispatch', () => {
const mockUserAcceptTermAndCondition = jest.fn()
it('should set COOKIE_DEVELOPER_FIRST_TIME_LOGIN_COMPLETE cookie', () => {
const TIME_OFFSET = 0
MockDate.set('2019-12-18T16:30:00', TIME_OFFSET)

const mockHistory = {
push: jest.fn(),
}
const fn = handleUserAccept(mockUserAcceptTermAndCondition, mockHistory)
const fn = handleUserAccept(mockHistory)
fn()
expect(mockUserAcceptTermAndCondition).toBeCalled()
expect(mockHistory.push).toBeCalledWith(routes.DEVELOPER_MY_APPS)
})
})

describe('mapDispatchToProps', () => {
describe('login', () => {
it('should call dispatch correctly', () => {
const mockDispatch = jest.fn()
const { userAcceptTermAndCondition } = mapDispatchToProps(mockDispatch)
userAcceptTermAndCondition()
expect(mockDispatch).toBeCalled()
})
expect(setCookieString).toHaveBeenCalledWith(
COOKIE_DEVELOPER_FIRST_TIME_LOGIN_COMPLETE,
new Date(),
COOKIE_MAX_AGE_INFINITY,
)
})
it('should redirect to DEVELOPER_MY_APPS', () => {
const mockHistory = {
push: jest.fn(),
}
const fn = handleUserAccept(mockHistory)
fn()
expect(mockHistory.push).toBeCalledWith(routes.DEVELOPER_MY_APPS)
})
})
})
Loading

0 comments on commit 58578a1

Please sign in to comment.