From 3b0543eb6608f15bc6c813dc30f6c740b5152dc3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 21 Jun 2022 13:36:14 -0500 Subject: [PATCH] reducers/status-hover-card: add tests --- .../__tests__/status-hover-card.test.tsx | 72 +++++++++++++++++++ app/soapbox/reducers/status-hover-card.ts | 5 +- 2 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 app/soapbox/reducers/__tests__/status-hover-card.test.tsx diff --git a/app/soapbox/reducers/__tests__/status-hover-card.test.tsx b/app/soapbox/reducers/__tests__/status-hover-card.test.tsx new file mode 100644 index 0000000000..9983b5b147 --- /dev/null +++ b/app/soapbox/reducers/__tests__/status-hover-card.test.tsx @@ -0,0 +1,72 @@ +import { + STATUS_HOVER_CARD_OPEN, + STATUS_HOVER_CARD_CLOSE, + STATUS_HOVER_CARD_UPDATE, +} from 'soapbox/actions/status-hover-card'; + +import reducer, { ReducerRecord } from '../status-hover-card'; + +describe(STATUS_HOVER_CARD_OPEN, () => { + it('sets the ref and statusId', () => { + const ref = { current: document.createElement('div') }; + + const action = { + type: STATUS_HOVER_CARD_OPEN, + ref, + statusId: '1234', + }; + + const result = reducer(undefined, action); + expect(result.ref).toBe(ref); + expect(result.statusId).toBe('1234'); + }); +}); + +describe(STATUS_HOVER_CARD_CLOSE, () => { + it('flushes the state', () => { + const state = ReducerRecord({ + ref: { current: document.createElement('div') }, + statusId: '1234', + }); + + const action = { type: STATUS_HOVER_CARD_CLOSE }; + + const result = reducer(state, action); + expect(result.ref).toBe(null); + expect(result.statusId).toBe(''); + }); + + it('leaves the state alone if hovered', () => { + const state = ReducerRecord({ + ref: { current: document.createElement('div') }, + statusId: '1234', + hovered: true, + }); + + const action = { type: STATUS_HOVER_CARD_CLOSE }; + const result = reducer(state, action); + expect(result).toEqual(state); + }); + + it('action.force flushes the state even if hovered', () => { + const state = ReducerRecord({ + ref: { current: document.createElement('div') }, + statusId: '1234', + hovered: true, + }); + + const action = { type: STATUS_HOVER_CARD_CLOSE, force: true }; + const result = reducer(state, action); + expect(result.ref).toBe(null); + expect(result.statusId).toBe(''); + }); +}); + +describe(STATUS_HOVER_CARD_UPDATE, () => { + it('sets hovered', () => { + const state = ReducerRecord(); + const action = { type: STATUS_HOVER_CARD_UPDATE }; + const result = reducer(state, action); + expect(result.hovered).toBe(true); + }); +}); diff --git a/app/soapbox/reducers/status-hover-card.ts b/app/soapbox/reducers/status-hover-card.ts index 11c660592c..1d3d1da8d7 100644 --- a/app/soapbox/reducers/status-hover-card.ts +++ b/app/soapbox/reducers/status-hover-card.ts @@ -8,7 +8,7 @@ import { import type { AnyAction } from 'redux'; -const ReducerRecord = ImmutableRecord({ +export const ReducerRecord = ImmutableRecord({ ref: null as React.MutableRefObject | null, statusId: '', hovered: false, @@ -26,7 +26,7 @@ export default function statusHoverCard(state: State = ReducerRecord(), action: case STATUS_HOVER_CARD_UPDATE: return state.set('hovered', true); case STATUS_HOVER_CARD_CLOSE: - if (state.get('hovered') === true && !action.force) + if (state.hovered === true && !action.force) return state; else return ReducerRecord(); @@ -34,4 +34,3 @@ export default function statusHoverCard(state: State = ReducerRecord(), action: return state; } } -