@@ -2,7 +2,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
22import {
33 parseScreenTree ,
44 RecursiveTreeViewBuilder ,
5- RecursiveAppendDuplicateFileMacros
5+ RecursiveAppendDuplicateFileMacros ,
6+ buildUrlId
67} from "../utils/parser" ;
78import { FileIDs } from "../store" ;
89
@@ -60,6 +61,41 @@ describe("parseScreenTree()", (): void => {
6061 expect ( tree . length ) . toEqual ( 1 ) ;
6162 expect ( firstFile ) . toEqual ( "top.bob" ) ;
6263 } ) ;
64+
65+ it ( "successfully fetches the file with a display name" , async ( ) : Promise < void > => {
66+ const mockSuccessResponse = JSON . parse ( `
67+ {
68+ "file": "top.bob",
69+ "displayName": "Top Synoptic",
70+ "children": [
71+ {
72+ "file": "middle1.bob",
73+ "displayName": "Middle File 1",
74+ "children": [
75+ {
76+ "file": "bottom.bob",
77+ "displayName": "Bottom File"
78+ }
79+ ]
80+ },
81+ {
82+ "file": "middle2.bob",
83+ "displayName": "Middle File 2"
84+ }
85+ ]
86+ }` ) ;
87+ const mockJsonPromise = Promise . resolve ( mockSuccessResponse ) ;
88+ const mockFetchPromise = Promise . resolve ( {
89+ json : ( ) : Promise < unknown > => mockJsonPromise
90+ } ) ;
91+ const mockFetch = ( ) : Promise < unknown > => mockFetchPromise ;
92+ vi . spyOn ( globalWithFetch , "fetch" ) . mockImplementation ( mockFetch ) ;
93+
94+ const [ tree , , firstFile ] = await parseScreenTree ( "test-map.json" ) ;
95+ expect ( tree . length ) . toEqual ( 1 ) ;
96+ expect ( firstFile ) . toEqual ( "top.bob" ) ;
97+ expect ( tree [ 0 ] . label ) . toEqual ( "Top Synoptic" ) ;
98+ } ) ;
6399} ) ;
64100
65101describe ( "RecursiveTreeViewBuilder()" , ( ) : void => {
@@ -219,6 +255,58 @@ describe("RecursiveTreeViewBuilder()", (): void => {
219255
220256 expect ( actualUrlIds . sort ( ) ) . toEqual ( expectedUrlIds . sort ( ) ) ;
221257 } ) ;
258+
259+ it ( "parses child screens to generate the expected urlId strings with display names" , async ( ) : Promise < void > => {
260+ const testJson = {
261+ file : "top.bob" ,
262+ displayName : "Top Screen" ,
263+ children : [
264+ {
265+ file : "middle1.bob" ,
266+ displayName : "Middle Screen" ,
267+ children : [
268+ {
269+ file : "path1/index.bob" ,
270+ displayName : "Index 1"
271+ } ,
272+ {
273+ file : "path2/index.bob" ,
274+ displayName : "Index 2"
275+ } ,
276+ {
277+ file : "index.bob" ,
278+ displayName : "Index 3"
279+ } ,
280+ {
281+ file : "path1/not_index.bob" ,
282+ displayName : "Not Index"
283+ }
284+ ]
285+ } ,
286+ {
287+ file : "path0/index.bob" ,
288+ displayName : "Index"
289+ }
290+ ]
291+ } ;
292+
293+ const { fileMap } = await RecursiveTreeViewBuilder (
294+ testJson . children ,
295+ "Top Screen"
296+ ) ;
297+
298+ const expectedUrlIds = [
299+ "Top Screen+Index" ,
300+ "Top Screen+Middle Screen" ,
301+ "Top Screen+Middle Screen+Index 1" ,
302+ "Top Screen+Middle Screen+Index 2" ,
303+ "Top Screen+Middle Screen+Index 3" ,
304+ "Top Screen+Middle Screen+Not Index"
305+ ] ;
306+ const actualUrlIds = Object . values ( fileMap ) . map ( x => x . urlId ) ;
307+
308+ expect ( actualUrlIds . sort ( ) ) . toEqual ( expectedUrlIds . sort ( ) ) ;
309+ } ) ;
222310} ) ;
223311
224312describe ( "RecursiveAppendDuplicateFileMacros()" , ( ) : void => {
@@ -351,3 +439,30 @@ describe("RecursiveAppendDuplicateFileMacros()", (): void => {
351439 expect ( aMacros ) . toEqual ( { Another : "AnotherFilesMacros" } ) ;
352440 } ) ;
353441} ) ;
442+
443+ describe ( "buildUrlId()" , ( ) : void => {
444+ it ( "uses the file name if no display name" , ( ) : void => {
445+ const { urlId, fileLabel } = buildUrlId ( "testing.bob" , "" , undefined ) ;
446+
447+ expect ( urlId ) . toBe ( "testing" ) ;
448+ expect ( fileLabel ) . toBe ( "testing" ) ;
449+ } ) ;
450+
451+ it ( "uses display name as url id" , ( ) : void => {
452+ const { urlId, fileLabel } = buildUrlId ( "testing.bob" , "" , "My File" ) ;
453+
454+ expect ( urlId ) . toBe ( "My File" ) ;
455+ expect ( fileLabel ) . toBe ( "My File" ) ;
456+ } ) ;
457+
458+ it ( "correctly attaches a prefix" , ( ) : void => {
459+ const { urlId, fileLabel } = buildUrlId (
460+ "testing.bob" ,
461+ "First File" ,
462+ "My File"
463+ ) ;
464+
465+ expect ( urlId ) . toBe ( "First File+My File" ) ;
466+ expect ( fileLabel ) . toBe ( "My File" ) ;
467+ } ) ;
468+ } ) ;
0 commit comments