From 057595ce3fbae2e6669ae6155f915be173b88f26 Mon Sep 17 00:00:00 2001 From: Jessica McInchak Date: Thu, 12 Oct 2023 11:25:47 +0200 Subject: [PATCH 1/2] simplest test case --- .../lib/__tests__/autoAnswers.test.ts | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 editor.planx.uk/src/pages/FlowEditor/lib/__tests__/autoAnswers.test.ts diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/autoAnswers.test.ts b/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/autoAnswers.test.ts new file mode 100644 index 0000000000..507a3ec05b --- /dev/null +++ b/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/autoAnswers.test.ts @@ -0,0 +1,79 @@ +import { TYPES } from "@planx/components/types"; + +import { Store, vanillaStore } from "../store"; + +const { getState, setState } = vanillaStore; + +const flow: Store.flow = { + _root: { + edges: ["SetValue", "Content", "AutomatedQuestion"], + }, + ResponseApple: { + data: { + val: "apple", + text: "Apple", + }, + type: TYPES.Response, + }, + ResponsePear: { + data: { + val: "pear", + text: "Pear", + }, + type: TYPES.Response, + }, + SetValue: { + data: { + fn: "fruit", + val: "apple", + }, + type: TYPES.SetValue, + }, + AutomatedQuestion: { + data: { + fn: "fruit", + text: "Which fruit?", + }, + type: TYPES.Statement, + edges: ["ResponseApple", "ResponsePear"], + }, + Content: { + data: { + content: "

Pause

", + }, + type: TYPES.Content, + }, +}; + +beforeEach(() => { + getState().resetPreview(); +}); + +test.skip("A question is auto-answered when it is reached, not when its' `fn` is first added to the breadcrumbs/passport", () => { + setState({ + flow, + }); + + expect(getState().upcomingCardIds()).toEqual([ + "SetValue", + "Content", + "AutomatedQuestion", + ]); + + // One step forwards + getState().record("SetValue", { data: { fruit: ["apple"] }, auto: true }); + expect(getState().breadcrumbs).toMatchObject({ + SetValue: { + auto: true, + data: { + fruit: ["apple"], + }, + }, + }); + + // "AutomatedQuestion" should still be queued up, not already answered + expect(getState().upcomingCardIds()).toEqual([ + "Content", + "AutomatedQuestion", + ]); +}); From f9504f95fa060b218dae1f6617594da3d613a82a Mon Sep 17 00:00:00 2001 From: Jessica McInchak Date: Tue, 24 Oct 2023 21:42:21 +0200 Subject: [PATCH 2/2] better test --- .../lib/__tests__/autoAnswers.test.ts | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/autoAnswers.test.ts b/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/autoAnswers.test.ts index 507a3ec05b..db2aa89a7c 100644 --- a/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/autoAnswers.test.ts +++ b/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/autoAnswers.test.ts @@ -3,6 +3,7 @@ import { TYPES } from "@planx/components/types"; import { Store, vanillaStore } from "../store"; const { getState, setState } = vanillaStore; +const { upcomingCardIds, resetPreview, record, currentCard } = getState(); const flow: Store.flow = { _root: { @@ -46,34 +47,37 @@ const flow: Store.flow = { }; beforeEach(() => { - getState().resetPreview(); + resetPreview(); + setState({ flow }); }); test.skip("A question is auto-answered when it is reached, not when its' `fn` is first added to the breadcrumbs/passport", () => { - setState({ - flow, - }); + const visitedNodes = () => Object.keys(getState().breadcrumbs); - expect(getState().upcomingCardIds()).toEqual([ + // mimic "Continue" button and properly set visitedNodes() + const clickContinue = () => upcomingCardIds(); + + expect(upcomingCardIds()).toEqual([ "SetValue", "Content", "AutomatedQuestion", ]); - // One step forwards - getState().record("SetValue", { data: { fruit: ["apple"] }, auto: true }); - expect(getState().breadcrumbs).toMatchObject({ - SetValue: { - auto: true, - data: { - fruit: ["apple"], - }, - }, - }); + // Step forwards through the SetValue + record("SetValue", { data: { fruit: ["apple"] }, auto: true }); + clickContinue(); - // "AutomatedQuestion" should still be queued up, not already answered - expect(getState().upcomingCardIds()).toEqual([ - "Content", - "AutomatedQuestion", - ]); + expect(currentCard()?.id).toBe("Content"); + + // "AutomatedQuestion" should still be queued up, not already answered based on SetValue + expect(visitedNodes()).not.toContain("AutomatedQuestion"); + expect(upcomingCardIds()).toContain("AutomatedQuestion"); + + // Step forwards through Content + record("Content", { data: {}, auto: false }); + clickContinue(); + + // "AutomatedQuestion" has now been auto-answered now, end of flow + expect(visitedNodes()).toContain("AutomatedQuestion"); + expect(upcomingCardIds()).toEqual([]); });