|
| 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 | +}) |
0 commit comments