Skip to content

Commit dfe7c20

Browse files
authored
feat: add unit tests to quartzOrganizations reducer (#5107)
1 parent b517e30 commit dfe7c20

File tree

3 files changed

+165
-2
lines changed

3 files changed

+165
-2
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export const mockOrgData = [
2+
{
3+
id: '9296169091c64567',
4+
name: 'Test Co. 1',
5+
isDefault: true,
6+
isActive: true,
7+
},
8+
{
9+
id: 'a71ced2b8238902b',
10+
name: 'Test Corp. 2',
11+
isDefault: false,
12+
isActive: false,
13+
},
14+
{
15+
id: 'ac3d3c04b8f1a545',
16+
name: 'Test GmbH 3',
17+
isDefault: false,
18+
isActive: false,
19+
},
20+
{
21+
id: 'fc734484afa0fcac',
22+
name: 'Test Inc. 4',
23+
isDefault: false,
24+
isActive: false,
25+
},
26+
{
27+
id: '62cba0af4760ce02',
28+
name: 'Test SA 5',
29+
isDefault: false,
30+
isActive: false,
31+
},
32+
]
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Reducer
2+
import reducer from 'src/identity/quartzOrganizations/reducers'
3+
import {initialState} from 'src/identity/quartzOrganizations/reducers'
4+
5+
// Actions
6+
import {
7+
setQuartzDefaultOrg,
8+
setQuartzOrganizations,
9+
} from 'src/identity/quartzOrganizations/actions/creators'
10+
11+
// Mocks
12+
import {mockOrgData} from 'src/identity/quartzOrganizations/mockOrgData'
13+
14+
// Types
15+
import {RemoteDataState} from 'src/types'
16+
17+
// Utils
18+
import {cloneDeep} from 'lodash'
19+
20+
describe('identity reducer for quartz organizations', () => {
21+
describe('loads orgs into state', () => {
22+
it('initializes a default state', () => {
23+
const oldState = reducer(
24+
undefined,
25+
setQuartzOrganizations(initialState.orgs)
26+
)
27+
expect(oldState.orgs).toStrictEqual(initialState.orgs)
28+
})
29+
30+
it('initializes a state that includes an array of orgs', () => {
31+
const oldState = reducer(
32+
initialState,
33+
setQuartzOrganizations(mockOrgData)
34+
)
35+
36+
expect(oldState.orgs).toStrictEqual(mockOrgData)
37+
})
38+
39+
it('sets a `Done` state when the organization array is loaded into state', () => {
40+
const newState = reducer(undefined, setQuartzOrganizations(mockOrgData))
41+
42+
expect(newState.status).toEqual(RemoteDataState.Done)
43+
})
44+
})
45+
46+
describe('changes default orgs', () => {
47+
let oldState
48+
49+
beforeEach(() => {
50+
oldState = reducer(initialState, setQuartzOrganizations(mockOrgData))
51+
})
52+
53+
describe('changes which org is the default org', () => {
54+
it('sets a new org as the `default` org', () => {
55+
const newDefaultOrg = oldState.orgs[1]
56+
57+
const newState = reducer(
58+
oldState,
59+
setQuartzDefaultOrg(newDefaultOrg.id)
60+
)
61+
62+
expect(newState.orgs[0].isDefault).toEqual(false)
63+
expect(newState.orgs[1].isDefault).toEqual(true)
64+
})
65+
66+
it('sets one - and only one - default org at once', () => {
67+
let currentState = oldState
68+
69+
mockOrgData.forEach(org => {
70+
const nextOrgId = org.id
71+
currentState = reducer(currentState, setQuartzDefaultOrg(nextOrgId))
72+
73+
const numDefaultOrgs = currentState.orgs.reduce((acc, org) => {
74+
return org.isDefault === true ? acc + 1 : acc
75+
}, 0)
76+
77+
expect(numDefaultOrgs).toEqual(1)
78+
})
79+
})
80+
})
81+
82+
it('leaves state unchanged if the `new` default org `is` the existing default', () => {
83+
const newOrgId = mockOrgData[0].id
84+
const newState = reducer(oldState, setQuartzDefaultOrg(newOrgId))
85+
86+
expect(oldState).toStrictEqual(newState)
87+
})
88+
})
89+
90+
describe('sets an appropriate `status` based on whether or not the new default org could be set', () => {
91+
let oldState
92+
93+
beforeEach(() => {
94+
oldState = reducer(undefined, setQuartzOrganizations(mockOrgData))
95+
})
96+
97+
it('sets a `Done` state if the current default org was updated', () => {
98+
const newId = mockOrgData[2].id
99+
const newState = reducer(oldState, setQuartzDefaultOrg(newId))
100+
101+
expect(newState.status).toEqual(RemoteDataState.Done)
102+
})
103+
104+
it('sets an `Error` state if the current default org was `not` updated', () => {
105+
const newId = 'fake id'
106+
const newState = reducer(oldState, setQuartzDefaultOrg(newId))
107+
108+
expect(newState.status).toEqual(RemoteDataState.Error)
109+
})
110+
111+
it('sets an `Error` state if there is no existing `default` org', () => {
112+
const mockOrgClone = cloneDeep(mockOrgData)
113+
mockOrgClone[0].isDefault = false
114+
115+
const newOrgId = mockOrgClone[3].id
116+
117+
oldState = reducer(oldState, setQuartzOrganizations(mockOrgClone))
118+
const newState = reducer(oldState, setQuartzDefaultOrg(newOrgId))
119+
120+
expect(newState.status).toEqual(RemoteDataState.Error)
121+
expect(newState.orgs[0].isDefault).toEqual(false)
122+
expect(newState.orgs[3].isDefault).toEqual(false)
123+
})
124+
})
125+
})

src/identity/quartzOrganizations/reducers/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import produce from 'immer'
1212

1313
import {OrganizationSummaries} from 'src/client/unityRoutes'
1414
import {RemoteDataState} from 'src/types'
15+
1516
export const initialState = {
1617
orgs: [emptyOrg] as OrganizationSummaries,
18+
status: RemoteDataState.NotStarted,
1719
} as QuartzOrganizations
1820

1921
export default (state = initialState, action: Actions): QuartzOrganizations =>
@@ -32,11 +34,15 @@ export default (state = initialState, action: Actions): QuartzOrganizations =>
3234
}
3335

3436
case SET_QUARTZ_DEFAULT_ORG: {
35-
// No existing default org is acceptable; oldDefaultOrg may be undefined.
3637
const oldDefaultOrg = draftState.orgs.find(
3738
org => org.isDefault === true
3839
)
39-
const oldDefaultOrgId = oldDefaultOrg?.id
40+
if (oldDefaultOrg === undefined) {
41+
draftState.status = RemoteDataState.Error
42+
return
43+
}
44+
45+
const oldDefaultOrgId = oldDefaultOrg.id
4046
const {newDefaultOrgId} = action
4147

4248
if (oldDefaultOrgId === newDefaultOrgId) {

0 commit comments

Comments
 (0)