|
| 1 | +// tests/followSequence.test.js |
| 2 | + |
| 3 | +import React from 'react'; |
| 4 | +import { followSequence } from '../testUtils/followSequence'; |
| 5 | +import type { SequenceChild } from '../types'; |
| 6 | + |
| 7 | +describe('followSequence', () => { |
| 8 | + beforeEach(() => { |
| 9 | + jest.clearAllMocks(); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should return all required pages in order for a simple linear sequence', () => { |
| 13 | + const pages = [ |
| 14 | + { |
| 15 | + id: 'page1', |
| 16 | + isComplete: () => false, |
| 17 | + Component: () => <div />, |
| 18 | + }, |
| 19 | + { |
| 20 | + id: 'page2', |
| 21 | + isComplete: () => false, |
| 22 | + Component: () => <div />, |
| 23 | + }, |
| 24 | + { |
| 25 | + id: 'page3', |
| 26 | + isComplete: () => false, |
| 27 | + Component: () => <div />, |
| 28 | + }, |
| 29 | + ]; |
| 30 | + |
| 31 | + const data = {}; |
| 32 | + |
| 33 | + const visitedPages = followSequence(pages, data); |
| 34 | + |
| 35 | + expect(visitedPages.map((page) => page.id)).toEqual([ |
| 36 | + 'page1', |
| 37 | + 'page2', |
| 38 | + 'page3', |
| 39 | + ]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should skip pages where isRequired returns false', () => { |
| 43 | + const pages = [ |
| 44 | + { |
| 45 | + id: 'page1', |
| 46 | + isRequired: () => true, |
| 47 | + isComplete: () => false, |
| 48 | + Component: () => <div />, |
| 49 | + }, |
| 50 | + { |
| 51 | + id: 'page2', |
| 52 | + isRequired: () => false, |
| 53 | + isComplete: () => false, |
| 54 | + Component: () => <div />, |
| 55 | + }, |
| 56 | + { |
| 57 | + id: 'page3', |
| 58 | + isRequired: () => true, |
| 59 | + isComplete: () => false, |
| 60 | + Component: () => <div />, |
| 61 | + }, |
| 62 | + ]; |
| 63 | + |
| 64 | + const data = {}; |
| 65 | + |
| 66 | + const visitedPages = followSequence(pages, data); |
| 67 | + |
| 68 | + expect(visitedPages.map((page) => page.id)).toEqual(['page1', 'page3']); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should navigate to alternate next page when alternateNextPage returns a valid page ID', () => { |
| 72 | + const pages = [ |
| 73 | + { |
| 74 | + id: 'page1', |
| 75 | + alternateNextPage: () => 'page3', |
| 76 | + isComplete: () => false, |
| 77 | + Component: () => <div />, |
| 78 | + }, |
| 79 | + { id: 'page2', isComplete: () => false, Component: () => <div /> }, |
| 80 | + { id: 'page3', isComplete: () => false, Component: () => <div /> }, |
| 81 | + { id: 'page4', isComplete: () => false, Component: () => <div /> }, |
| 82 | + ]; |
| 83 | + |
| 84 | + const data = {}; |
| 85 | + |
| 86 | + const visitedPages = followSequence(pages, data); |
| 87 | + |
| 88 | + expect(visitedPages.map((page) => page.id)).toEqual([ |
| 89 | + 'page1', |
| 90 | + 'page3', |
| 91 | + 'page4', |
| 92 | + ]); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should stop navigation when a page with isFinal returns true', () => { |
| 96 | + const pages = [ |
| 97 | + { id: 'page1', isComplete: () => false, Component: () => <div /> }, |
| 98 | + { |
| 99 | + id: 'page2', |
| 100 | + isFinal: () => true, |
| 101 | + isComplete: () => false, |
| 102 | + Component: () => <div />, |
| 103 | + }, |
| 104 | + { id: 'page3', isComplete: () => false, Component: () => <div /> }, |
| 105 | + ]; |
| 106 | + |
| 107 | + const data = {}; |
| 108 | + |
| 109 | + const visitedPages = followSequence(pages, data); |
| 110 | + |
| 111 | + expect(visitedPages.map((page) => page.id)).toEqual(['page1', 'page2']); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should proceed to next page when alternateNextPage returns an invalid page ID', () => { |
| 115 | + console.warn = jest.fn(); |
| 116 | + |
| 117 | + const pages = [ |
| 118 | + { |
| 119 | + id: 'page1', |
| 120 | + alternateNextPage: () => 'invalidPage', |
| 121 | + isComplete: () => false, |
| 122 | + Component: () => <div />, |
| 123 | + }, |
| 124 | + { id: 'page2', isComplete: () => false, Component: () => <div /> }, |
| 125 | + { id: 'page3', isComplete: () => false, Component: () => <div /> }, |
| 126 | + ]; |
| 127 | + |
| 128 | + const data = {}; |
| 129 | + |
| 130 | + const visitedPages = followSequence(pages, data); |
| 131 | + |
| 132 | + expect(visitedPages.map((page) => page.id)).toEqual([ |
| 133 | + 'page1', |
| 134 | + 'page2', |
| 135 | + 'page3', |
| 136 | + ]); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should handle data-dependent isRequired and alternateNextPage functions', () => { |
| 140 | + const formData = { includePage1: true, skipToPage4: true }; |
| 141 | + |
| 142 | + const pages = [ |
| 143 | + { |
| 144 | + id: 'page1', |
| 145 | + isRequired: (data: Partial<typeof formData>) => |
| 146 | + data.includePage1, |
| 147 | + isComplete: () => false, |
| 148 | + Component: () => <div />, |
| 149 | + }, |
| 150 | + { |
| 151 | + id: 'page2', |
| 152 | + alternateNextPage: (data: Partial<typeof formData>) => |
| 153 | + data.skipToPage4 ? 'page4' : undefined, |
| 154 | + isComplete: () => false, |
| 155 | + Component: () => <div />, |
| 156 | + }, |
| 157 | + { id: 'page3', isComplete: () => false, Component: () => <div /> }, |
| 158 | + { id: 'page4', isComplete: () => false, Component: () => <div /> }, |
| 159 | + ]; |
| 160 | + |
| 161 | + const visitedPages = followSequence(pages, formData); |
| 162 | + |
| 163 | + expect(visitedPages.map((page) => page.id)).toEqual([ |
| 164 | + 'page1', |
| 165 | + 'page2', |
| 166 | + 'page4', |
| 167 | + ]); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should handle an empty sequence', () => { |
| 171 | + const pages: SequenceChild<any, any, any>[] = []; |
| 172 | + |
| 173 | + const data = {}; |
| 174 | + |
| 175 | + const visitedPages = followSequence(pages, data); |
| 176 | + |
| 177 | + expect(visitedPages).toEqual([]); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should prevent infinite loops when alternateNextPage creates a loop', () => { |
| 181 | + const pages = [ |
| 182 | + { |
| 183 | + id: 'page1', |
| 184 | + alternateNextPage: () => 'page2', |
| 185 | + isComplete: () => false, |
| 186 | + Component: () => <div />, |
| 187 | + }, |
| 188 | + { |
| 189 | + id: 'page2', |
| 190 | + alternateNextPage: () => 'page1', |
| 191 | + isComplete: () => false, |
| 192 | + Component: () => <div />, |
| 193 | + }, |
| 194 | + { id: 'page3', isComplete: () => false, Component: () => <div /> }, |
| 195 | + ]; |
| 196 | + |
| 197 | + const data = {}; |
| 198 | + |
| 199 | + expect(() => |
| 200 | + followSequence(pages, data), |
| 201 | + ).toThrowErrorMatchingInlineSnapshot( |
| 202 | + `"Loop detected at page 'page1'. Navigation stopped. Full path: page1 -> page2 -> page1"`, |
| 203 | + ); |
| 204 | + }); |
| 205 | + |
| 206 | + it('should handle pages with isFinal depending on data', () => { |
| 207 | + const formData = { endHere: true }; |
| 208 | + |
| 209 | + const pages = [ |
| 210 | + { |
| 211 | + id: 'page1', |
| 212 | + isFinal: (data: Partial<typeof formData>) => !!data.endHere, |
| 213 | + isComplete: () => false, |
| 214 | + Component: () => <div />, |
| 215 | + }, |
| 216 | + { id: 'page2', isComplete: () => false, Component: () => <div /> }, |
| 217 | + ]; |
| 218 | + |
| 219 | + const visitedPages = followSequence(pages, formData); |
| 220 | + |
| 221 | + expect(visitedPages.map((page) => page.id)).toEqual(['page1']); |
| 222 | + }); |
| 223 | + |
| 224 | + it('should continue navigation when isFinal returns false', () => { |
| 225 | + const pages = [ |
| 226 | + { |
| 227 | + id: 'page1', |
| 228 | + isFinal: () => false, |
| 229 | + isComplete: () => false, |
| 230 | + Component: () => <div />, |
| 231 | + }, |
| 232 | + { id: 'page2', isComplete: () => false, Component: () => <div /> }, |
| 233 | + ]; |
| 234 | + |
| 235 | + const data = {}; |
| 236 | + |
| 237 | + const visitedPages = followSequence(pages, data); |
| 238 | + |
| 239 | + expect(visitedPages.map((page) => page.id)).toEqual(['page1', 'page2']); |
| 240 | + }); |
| 241 | +}); |
0 commit comments