Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: #1127 update test, var name
Browse files Browse the repository at this point in the history
Truong An committed Jun 2, 2020
1 parent d4e360f commit b9081d9
Showing 11 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -8,10 +8,13 @@ import ClientAppDetail, {
handleCloseInstallConfirmationModal,
handleInstallAppButtonClick,
renderAppHeaderButtonGroup,
handleApplyAppDetailsFromLocalStorage,
} from '../client-app-detail'
import { Button } from '@reapit/elements'
import Routes from '@/constants/routes'
import appState from '@/reducers/__stubs__/app-state'
import { developerApplyAppDetails } from '@/actions/developer'
import { AppDetailData } from '@/reducers/client/app-detail'

describe('ClientAppDetail', () => {
let store
@@ -169,4 +172,18 @@ describe('ClientAppDetail', () => {
expect(mockFunction).toBeCalledWith(true)
})
})

describe('handleApplyAppDetailsFromLocalStorage', () => {
it('should run correctly', () => {
const dispatch = jest.fn()
const appId = 'appId'
const value = { id: 'appId' } as AppDetailData
const stringValue = JSON.stringify(value)
const spyLocalStorageGetItem = jest.spyOn(window.localStorage, 'getItem').mockImplementation(() => stringValue)
const fn = handleApplyAppDetailsFromLocalStorage(dispatch, appId)
fn()
expect(spyLocalStorageGetItem).toBeCalledWith('developer-preview-app')
expect(dispatch).toBeCalledWith(developerApplyAppDetails(value))
})
})
})
Original file line number Diff line number Diff line change
@@ -36,15 +36,15 @@ export const handleInstallAppButtonClick = (setIsVisibleInstallConfirmation: (is
}
}

export const handleApplyAppDetailsFromLocalStorage = (dispatch: Dispatch, appid?: string) => () => {
export const handleApplyAppDetailsFromLocalStorage = (dispatch: Dispatch, appId?: string) => () => {
try {
const appDataString = localStorage.getItem('developer-preview-app')
if (!appDataString) {
throw 'No app preview'
}

const appData = JSON.parse(appDataString)
if (appData.id !== appid) {
if (appData.id !== appId) {
throw 'No app preview'
}

@@ -94,7 +94,7 @@ export const renderAppHeaderButtonGroup = (

const ClientAppDetail: React.FC<ClientAppDetailProps> = () => {
const dispatch = useDispatch()
const { id: appid } = useParams()
const { appId } = useParams()

const [isVisibleInstallConfirmation, setIsVisibleInstallConfirmation] = React.useState(false)
const [isVisibleUninstallConfirmation, setIsVisibleUninstallConfirmation] = React.useState(false)
@@ -129,7 +129,7 @@ const ClientAppDetail: React.FC<ClientAppDetailProps> = () => {
const unfetched = Object.keys(appDetailData).length === 0
const { id = '', installedOn = '' } = appDetailData

React.useEffect(handleApplyAppDetailsFromLocalStorage(dispatch, appid), [dispatch])
React.useEffect(handleApplyAppDetailsFromLocalStorage(dispatch, appId), [dispatch])

if (error) return <Alert message={error} type="danger"></Alert>
if (isLoadingAppDetail || unfetched) {
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import DeveloperSubmitApp, {
handleGoBackToApps,
handleOnSubmitAnotherApp,
generateInitialValues,
handleOpenAppPreview,
} from '../developer-submit-app'
import { getMockRouterProps } from '@/utils/mock-helper'
import { FIELD_ERROR_DESCRIPTION } from '@/constants/form'
@@ -292,4 +293,18 @@ describe('DeveloperSubmitApp', () => {
})
})
})

describe('handleOpenAppPreview', () => {
it('should run correctly', () => {
const params = { appDetails: {}, values: {}, scopes: [], appId: 'appId' }
const spyLocalStorageSetItem = jest.spyOn(window.localStorage, 'setItem')
const spyOpenUrl = jest.spyOn(window, 'open')
const expected = JSON.stringify({ scopes: [] })

const fn = handleOpenAppPreview(params)
fn()
expect(spyLocalStorageSetItem).toBeCalledWith('developer-preview-app', expected)
expect(spyOpenUrl).toBeCalledWith('developer/apps/appId/preview', '_blank')
})
})
})
Original file line number Diff line number Diff line change
@@ -279,18 +279,18 @@ export const handleOnSubmitAnotherApp = (dispatch: Dispatch) => {
export type HandleOpenAppPreview = {
scopes: ScopeModel[]
values: FormikValues
appid?: string
appId?: string
appDetails?: AppDetailModel & { apiKey?: string }
}

export const handleOpenAppPreview = ({ appDetails, values, scopes, appid }: HandleOpenAppPreview) => () => {
export const handleOpenAppPreview = ({ appDetails, values, scopes, appId }: HandleOpenAppPreview) => () => {
const appDetailState = {
...appDetails,
...values,
scopes: scopes.filter(scope => values.scopes.includes(scope.name)),
}

const url = `developer/apps/${appid}/preview`
const url = `developer/apps/${appId}/preview`
localStorage.setItem('developer-preview-app', JSON.stringify(appDetailState))
window.open(url, '_blank')
}
@@ -403,7 +403,7 @@ export const DeveloperSubmitApp: React.FC<DeveloperSubmitAppProps> = () => {
appDetails: appDetailState?.appDetailData?.data,
values,
scopes,
appid,
appId: appid,
})}
variant="primary"
type="button"
2 changes: 1 addition & 1 deletion packages/marketplace/src/constants/routes.ts
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ const Routes = {
SETTINGS: '/developer/settings',
SUBMIT_APP: '/developer/submit-app',
DEVELOPER_HELP: '/developer/help',
DEVELOPER_APP_PREVIEW: '/developer/apps/:id/preview',
DEVELOPER_APP_PREVIEW: '/developer/apps/:appId/preview',
CLIENT_HELP: '/client/help',
REGISTER: '/register',
REGISTER_CONFIRM: '/register/confirm',
Original file line number Diff line number Diff line change
@@ -389,7 +389,7 @@ exports[`Router should match a snapshot 1`] = `
}
}
exact={true}
path="/developer/apps/:id/preview"
path="/developer/apps/:appId/preview"
/>
<Component
allow="ADMIN"
2 changes: 1 addition & 1 deletion packages/marketplace/src/selector/client-app-detail.ts
Original file line number Diff line number Diff line change
@@ -17,5 +17,5 @@ export const selectAppDetailLoading = (state: ReduxState) => {
}

export const selectAppDetailError = (state: ReduxState): string | null => {
return state.client.appDetail.error || null
return state?.client?.appDetail?.error || null
}
2 changes: 1 addition & 1 deletion packages/marketplace/src/tests/badges/badge-branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/marketplace/src/tests/badges/badge-functions.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/marketplace/src/tests/badges/badge-lines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/marketplace/src/tests/badges/badge-statements.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b9081d9

Please sign in to comment.