forked from decaporg/decap-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove immutable from status and auth (decaporg#4727)
- Loading branch information
1 parent
045ee87
commit 583744e
Showing
9 changed files
with
66 additions
and
68 deletions.
There are no files selected for viewing
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
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
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
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
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
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
24 changes: 15 additions & 9 deletions
24
packages/netlify-cms-core/src/reducers/__tests__/auth.spec.ts
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,32 +1,38 @@ | ||
import { fromJS } from 'immutable'; | ||
import { authenticating, authenticate, authError, logout } from '../../actions/auth'; | ||
import auth, { defaultState } from '../auth'; | ||
|
||
describe('auth', () => { | ||
it('should handle an empty state', () => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore | ||
// @ts-ignore auth reducer doesn't accept empty action | ||
expect(auth(undefined, {})).toEqual(defaultState); | ||
}); | ||
|
||
it('should handle an authentication request', () => { | ||
expect(auth(undefined, authenticating())).toEqual(defaultState.set('isFetching', true)); | ||
expect(auth(undefined, authenticating())).toEqual({ | ||
...defaultState, | ||
isFetching: true, | ||
}); | ||
}); | ||
|
||
it('should handle authentication', () => { | ||
const user = { name: 'joe', token: 'token' }; | ||
expect(auth(undefined, authenticate(user))).toEqual(defaultState.set('user', fromJS(user))); | ||
expect(auth(undefined, authenticate(user))).toEqual({ | ||
...defaultState, | ||
user, | ||
}); | ||
}); | ||
|
||
it('should handle an authentication error', () => { | ||
expect(auth(undefined, authError(new Error('Bad credentials')))).toEqual( | ||
defaultState.set('error', 'Error: Bad credentials'), | ||
); | ||
expect(auth(undefined, authError(new Error('Bad credentials')))).toEqual({ | ||
...defaultState, | ||
error: 'Error: Bad credentials', | ||
}); | ||
}); | ||
|
||
it('should handle logout', () => { | ||
const user = { name: 'joe', token: 'token' }; | ||
const newState = auth(defaultState.set('user', fromJS(user)), logout()); | ||
expect(newState.get('user')).toBeUndefined(); | ||
const newState = auth({ ...defaultState, user }, logout()); | ||
expect(newState.user).toBeUndefined(); | ||
}); | ||
}); |
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
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,46 +1,37 @@ | ||
import { fromJS } from 'immutable'; | ||
import { produce } from 'immer'; | ||
import { STATUS_REQUEST, STATUS_SUCCESS, STATUS_FAILURE, StatusAction } from '../actions/status'; | ||
import { StaticallyTypedRecord } from '../types/immutable'; | ||
|
||
export type Status = StaticallyTypedRecord<{ | ||
export type Status = { | ||
isFetching: boolean; | ||
status: StaticallyTypedRecord<{ | ||
auth: StaticallyTypedRecord<{ status: boolean }>; | ||
api: StaticallyTypedRecord<{ status: boolean; statusPage: string }>; | ||
}>; | ||
status: { | ||
auth: { status: boolean }; | ||
api: { status: boolean; statusPage: string }; | ||
}; | ||
error: Error | undefined; | ||
}>; | ||
}; | ||
|
||
const defaultState = fromJS({ | ||
const defaultState: Status = { | ||
isFetching: false, | ||
status: { | ||
auth: { status: true }, | ||
api: { status: true, statusPage: '' }, | ||
}, | ||
error: undefined, | ||
}) as Status; | ||
}; | ||
|
||
const status = (state = defaultState, action: StatusAction) => { | ||
const status = produce((state: Status, action: StatusAction) => { | ||
switch (action.type) { | ||
case STATUS_REQUEST: | ||
return state.set('isFetching', true); | ||
state.isFetching = true; | ||
break; | ||
case STATUS_SUCCESS: | ||
return state.withMutations(map => { | ||
map.set('isFetching', false); | ||
map.set('status', fromJS(action.payload.status)); | ||
}); | ||
state.isFetching = false; | ||
state.status = action.payload.status; | ||
break; | ||
case STATUS_FAILURE: | ||
return state.withMutations(map => { | ||
map.set('isFetching', false); | ||
map.set('error', action.payload.error); | ||
}); | ||
default: | ||
return state; | ||
state.isFetching = false; | ||
state.error = action.payload.error; | ||
} | ||
}; | ||
|
||
export const selectStatus = (status: Status) => { | ||
return status.get('status').toJS(); | ||
}; | ||
}, defaultState); | ||
|
||
export default status; |