Skip to content

Commit

Permalink
chore: #2572 change to notification to display error (#2598)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuong Vu authored Sep 8, 2020
1 parent f27d7a5 commit a563f3c
Show file tree
Hide file tree
Showing 38 changed files with 472 additions and 12,775 deletions.
11 changes: 1 addition & 10 deletions packages/developer-portal/src/actions/__tests__/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { errorClearedComponent, errorClearedServer, errorThrownComponent, errorThrownServer } from '../error'
import { errorClearedComponent, errorClearedServer, errorThrownComponent } from '../error'
import ActionTypes from '../../constants/action-types'
import { ErrorData } from '../../reducers/error'
import errorMessages from '../../constants/error-messages'
Expand All @@ -22,13 +22,4 @@ describe('error actions', () => {
expect(errorThrownComponent.type).toEqual(ActionTypes.ERROR_THROWN_COMPONENT)
expect(errorThrownComponent(errorData).data).toEqual(errorData)
})

it('should create a errorThrownServer action', () => {
const errorData = {
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
} as ErrorData
expect(errorThrownServer.type).toEqual(ActionTypes.ERROR_THROWN_SERVER)
expect(errorThrownServer(errorData).data).toEqual(errorData)
})
})
1 change: 0 additions & 1 deletion packages/developer-portal/src/actions/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ import { ErrorData } from '../reducers/error'
export const errorClearedComponent = actionCreator<null>(ActionTypes.ERROR_CLEARED_COMPONENT)
export const errorClearedServer = actionCreator<null>(ActionTypes.ERROR_CLEARED_SERVER)
export const errorThrownComponent = actionCreator<ErrorData>(ActionTypes.ERROR_THROWN_COMPONENT)
export const errorThrownServer = actionCreator<ErrorData>(ActionTypes.ERROR_THROWN_SERVER)
36 changes: 0 additions & 36 deletions packages/developer-portal/src/reducers/__tests__/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,40 +44,4 @@ describe('error reducer', () => {
}
expect(newState).toEqual(expected)
})

it('should set a server error', () => {
const errorData = {
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
} as ErrorData
const newState = errorReducer(undefined, {
type: ActionTypes.ERROR_THROWN_SERVER as ActionType,
data: errorData,
})
const expected = {
...defaultState,
serverError: errorData,
}
expect(newState).toEqual(expected)
})

it('should clear a server error', () => {
const errorData = {
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
} as ErrorData

const newState = errorReducer(
{ ...defaultState, serverError: errorData },
{
type: ActionTypes.ERROR_CLEARED_SERVER as ActionType,
data: null,
},
)
const expected = {
...defaultState,
serverError: null,
}
expect(newState).toEqual(expected)
})
})
4 changes: 2 additions & 2 deletions packages/developer-portal/src/reducers/error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Action } from '../types/core'
import { isType } from '../utils/actions'
import { errorClearedComponent, errorClearedServer, errorThrownComponent, errorThrownServer } from '../actions/error'
import { errorClearedComponent, errorClearedServer, errorThrownComponent } from '../actions/error'

export interface ErrorData {
readonly status?: number
Expand All @@ -19,7 +19,7 @@ export const defaultState: ErrorState = {
}

const errorReducer = (state: ErrorState = defaultState, action: Action<any>): ErrorState => {
if (isType(action, errorClearedServer) || isType(action, errorThrownServer)) {
if (isType(action, errorClearedServer)) {
return {
...state,
serverError: action.data,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { errorThrownServer } from '@/actions/error'
import { notification } from '@reapit/elements'
import { developerSetStatusRequestSaga } from '../developer-set-status'
import { put, call } from '@redux-saga/core/effects'
import { cloneableGenerator } from '@redux-saga/testing-utils'
Expand Down Expand Up @@ -29,12 +29,10 @@ describe('developerSetStatusRequestSaga', () => {

expect(gen.next().value).toEqual(put(developerSetStatusRequestFailure()))
expect(gen.next().value).toEqual(
put(
errorThrownServer({
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
}),
),
call(notification.error, {
message: errorMessages.DEFAULT_SERVER_ERROR,
placement: 'bottomRight',
}),
)
})
})
39 changes: 19 additions & 20 deletions packages/developer-portal/src/sagas/__tests__/developer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import developerSagas, {
developerWebhookPing,
} from '../developer'
import ActionTypes from '@/constants/action-types'
import { errorThrownServer } from '@/actions/error'
import errorMessages from '@/constants/error-messages'
import { developerIdentity } from '../__stubs__/developer-identity'
import { billing } from '../__stubs__/billing'
Expand All @@ -37,13 +36,19 @@ import {
fetchBillingsByMonth,
} from '@/services/billing'
import { getDeveloperId } from '@/utils/session'
import { notification } from '@reapit/elements'

jest.mock('@/services/apps')
jest.mock('@/services/scopes')
jest.mock('@/services/developers')
jest.mock('@/services/billing')

jest.mock('@reapit/elements')
jest.mock('@reapit/elements', () => ({
...jest.requireActual('@reapit/elements'),
notification: {
error: jest.fn(),
},
}))

const params = { data: { page: 1 } }

Expand All @@ -62,12 +67,10 @@ describe('developer create', () => {
if (!clone.throw) throw new Error('Generator object cannot throw')
expect(clone.throw('error').value).toEqual(put(developerSetFormState('ERROR')))
expect(clone.next().value).toEqual(
put(
errorThrownServer({
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
}),
),
call(notification.error, {
message: errorMessages.DEFAULT_SERVER_ERROR,
placement: 'bottomRight',
}),
)
expect(clone.next().done).toEqual(true)
})
Expand Down Expand Up @@ -103,12 +106,10 @@ describe('fetchBillingSagas', () => {
if (!clone.throw) throw new Error('Generator object cannot throw')
expect(clone.throw('error').value).toEqual(put(fetchBillingFailure('error')))
expect(clone.next().value).toEqual(
put(
errorThrownServer({
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
}),
),
call(notification.error, {
message: errorMessages.DEFAULT_SERVER_ERROR,
placement: 'bottomRight',
}),
)
expect(clone.next().done).toEqual(true)
})
Expand All @@ -131,12 +132,10 @@ describe('fetchMonthlyBillingSagas', () => {
if (!clone.throw) throw new Error('Generator object cannot throw')
expect(clone.throw('error').value).toEqual(put(fetchMonthlyBillingFailure('error')))
expect(clone.next().value).toEqual(
put(
errorThrownServer({
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
}),
),
call(notification.error, {
message: errorMessages.DEFAULT_SERVER_ERROR,
placement: 'bottomRight',
}),
)
expect(clone.next().done).toEqual(true)
})
Expand Down
45 changes: 30 additions & 15 deletions packages/developer-portal/src/sagas/__tests__/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { DeveloperModel } from '@reapit/foundations-ts-definitions'
import { ChangePasswordParams, settingShowLoading, requestDeveloperDataSuccess } from '@/actions/settings'
import { cloneableGenerator } from '@redux-saga/testing-utils'
import { selectDeveloperEmail } from '@/selector/developer'
import { errorThrownServer } from '@/actions/error'
import { notification } from '@reapit/elements'

import errorMessages from '@/constants/error-messages'
import messages from '@/constants/messages'
Expand All @@ -35,6 +35,13 @@ jest.mock('@/services/cognito-identity', () => ({
changePasswordService: jest.fn().mockResolvedValue('SUCCESS'),
}))

jest.mock('@reapit/elements', () => ({
...jest.requireActual('@reapit/elements'),
notification: {
error: jest.fn(),
},
}))

describe('settings', () => {
describe('developerInformationFetch', () => {
const gen = cloneableGenerator(developerInformationFetch)()
Expand All @@ -55,12 +62,10 @@ describe('settings', () => {
const clone = gen.clone()
if (!clone.throw) throw new Error('Generator object cannot throw')
expect(clone.throw('error').value).toEqual(
put(
errorThrownServer({
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
}),
),
call(notification.error, {
message: errorMessages.DEFAULT_SERVER_ERROR,
placement: 'bottomRight',
}),
)
expect(clone.next().value).toEqual(put(settingShowLoading(false)))
expect(clone.next().done).toEqual(true)
Expand Down Expand Up @@ -108,12 +113,10 @@ describe('settings', () => {
const clone = gen.clone()
if (!clone.throw) throw new Error('Generator object cannot throw')
expect(clone.throw('error').value).toEqual(
put(
errorThrownServer({
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
}),
),
call(notification.error, {
message: errorMessages.DEFAULT_SERVER_ERROR,
placement: 'bottomRight',
}),
)
expect(clone.next().value).toEqual(put(settingShowLoading(false)))
expect(clone.next().done).toEqual(true)
Expand Down Expand Up @@ -152,14 +155,26 @@ describe('settings', () => {
}),
)
if (!clone.throw) throw new Error('Generator object cannot throw')
expect(clone.throw({ message: 'error message' }).value).toEqual(put(settingShowLoading(false)))
expect(clone.throw({ message: 'error message' }).value).toEqual(
call(notification.error, {
message: errorMessages.DEFAULT_SERVER_ERROR,
placement: 'bottomRight',
}),
)
expect(clone.next().value).toEqual(put(settingShowLoading(false)))
expect(clone.next().done).toEqual(true)
})

it('should call API fail', () => {
const clone = gen.clone()
if (!clone.throw) throw new Error('Generator object cannot throw')
expect(clone.throw({ message: 'error message' }).value).toEqual(put(settingShowLoading(false)))
expect(clone.throw({ message: 'error message' }).value).toEqual(
call(notification.error, {
message: errorMessages.DEFAULT_SERVER_ERROR,
placement: 'bottomRight',
}),
)
expect(clone.next().value).toEqual(put(settingShowLoading(false)))
expect(clone.next().done).toEqual(true)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { fetchAppDetailSaga } from '../app-detail'
import { put, call } from '@redux-saga/core/effects'
import { Action } from '@/types/core'
import { cloneableGenerator } from '@redux-saga/testing-utils'
import { errorThrownServer } from '@/actions/error'
import { notification } from '@reapit/elements'
import errorMessages from '@/constants/error-messages'
import { FetchAppByIdParams, fetchAppById } from '@/services/apps'
import { fetchApiKeyInstallationById } from '@/services/installations'
import { appDetailDataStub } from '@/sagas/__stubs__/app-detail'
import { fetchAppDetailSuccess } from '@/actions/apps'

jest.mock('@reapit/elements')
jest.mock('@reapit/elements', () => ({
...jest.requireActual('@reapit/elements'),
notification: {
error: jest.fn(),
},
}))

const paramsClientId: Action<FetchAppByIdParams> = {
data: { id: '9b6fd5f7-2c15-483d-b925-01b650538e52', clientId: 'DAC' },
Expand All @@ -36,12 +41,10 @@ describe('fetch developer app detail with clientId', () => {
const clone = gen.clone()
// @ts-ignore
expect(clone.throw('error').value).toEqual(
put(
errorThrownServer({
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
}),
),
call(notification.error, {
message: errorMessages.DEFAULT_SERVER_ERROR,
placement: 'bottomRight',
}),
)
})
})
Expand All @@ -59,12 +62,10 @@ describe('fetch developer app detail without clientId', () => {
const clone = gen.clone()
// @ts-ignore
expect(clone.throw('error').value).toEqual(
put(
errorThrownServer({
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
}),
),
call(notification.error, {
message: errorMessages.DEFAULT_SERVER_ERROR,
placement: 'bottomRight',
}),
)
})
})
Expand Down Expand Up @@ -102,12 +103,10 @@ describe('client app detail fetch data and fetch apiKey', () => {
const clone = gen.clone()
// @ts-ignore
expect(clone.throw('error').value).toEqual(
put(
errorThrownServer({
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
}),
),
call(notification.error, {
message: errorMessages.DEFAULT_SERVER_ERROR,
placement: 'bottomRight',
}),
)
})
})
12 changes: 5 additions & 7 deletions packages/developer-portal/src/sagas/apps/app-authentication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fetchtAppAuthentication, fetchtAppAuthenticationSuccess, fetchtAppAuthenticationFailed } from '@/actions/apps'
import { put, call, fork, takeLatest, all } from '@redux-saga/core/effects'
import { errorThrownServer } from '@/actions/error'
import { notification } from '@reapit/elements'
import errorMessages from '@/constants/error-messages'
import { Action } from '@/types/core'
import { fetchAppSecretByIdAPI } from '@/services/apps'
Expand All @@ -11,12 +11,10 @@ export const requestAuthCode = function*({ data: id }: Action<string>) {
yield put(fetchtAppAuthenticationSuccess(response))
} catch (err) {
yield put(fetchtAppAuthenticationFailed(err?.description))
yield put(
errorThrownServer({
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
}),
)
yield call(notification.error, {
message: err?.description ?? errorMessages.DEFAULT_SERVER_ERROR,
placement: 'bottomRight',
})
}
}

Expand Down
12 changes: 5 additions & 7 deletions packages/developer-portal/src/sagas/apps/app-detail.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// import { clientFetchAppDetailSuccess } from '@/actions/client'
import { put, call, fork, takeLatest, all } from '@redux-saga/core/effects'
import ActionTypes from '@/constants/action-types'
import { errorThrownServer } from '@/actions/error'
import { notification } from '@reapit/elements'
import errorMessages from '@/constants/error-messages'
import { Action } from '@/types/core'
import { fetchAppById, FetchAppByIdParams } from '@/services/apps'
Expand All @@ -19,12 +19,10 @@ export const fetchAppDetailSaga = function*({ data }: Action<FetchAppByIdParams>
}
yield put(fetchAppDetailSuccess(appDetailResponse))
} catch (err) {
yield put(
errorThrownServer({
type: 'SERVER',
message: errorMessages.DEFAULT_SERVER_ERROR,
}),
)
yield call(notification.error, {
message: err?.description ?? errorMessages.DEFAULT_SERVER_ERROR,
placement: 'bottomRight',
})
}
}

Expand Down
Loading

0 comments on commit a563f3c

Please sign in to comment.