1- import { render , screen , fireEvent , prettyDOM , within } from '@testing-library/react' ;
1+ import { render , screen , fireEvent , prettyDOM , within , cleanup } from '@testing-library/react' ;
22import { Maker } from '../../utils/pageImports' ;
33
44const mockCourse = {
@@ -332,4 +332,40 @@ describe('Maker', () => {
332332 expect ( screen . queryByTestId ( 'load-slot-4' ) ) . toBeInTheDocument ( ) ;
333333 expect ( screen . queryByText ( / L a y o u t 1 / ) ) . not . toBeInTheDocument ( ) ;
334334 } ) ;
335+
336+ it ( 'uses localStorage to save layouts on refresh' , ( ) => {
337+ const grid = screen . getByTestId ( 'grid' ) ;
338+ const layoutNameInput = screen . getByPlaceholderText ( 'Layout name' ) ;
339+ const saveLayout = screen . getByTestId ( 'save-layout' ) ;
340+
341+ // Have a saved layout and a layout in the grid
342+ fireEvent . change ( stringInput , { target : { value : mockLayouts . ECE } } ) ;
343+ fireEvent . click ( loadLayout ) ;
344+ fireEvent . change ( layoutNameInput , { target : { value : 'This exist??' } } ) ;
345+ fireEvent . click ( saveLayout ) ;
346+ fireEvent . change ( stringInput , { target : { value : mockLayouts . CE } } ) ;
347+ fireEvent . click ( loadLayout ) ;
348+
349+ expect ( screen . getByText ( / T h i s e x i s t \? \? / ) ) . toBeInTheDocument ( ) ;
350+ expect ( within ( grid ) . getByText ( / E C E 3 4 5 / ) ) . toBeInTheDocument ( ) ;
351+
352+ const layouts = JSON . parse ( localStorage . getItem ( 'savedLayouts' ) || '[]' ) ;
353+ expect ( layouts ) . toHaveLength ( 1 ) ;
354+
355+ // Make sure refresh works by expecting filter to be reset on refresh
356+ const filterSearch = screen . getByTestId ( 'filter-search' ) ;
357+ fireEvent . change ( filterSearch , { target : { value : 'uuuuuuuu' } } ) ;
358+ expect ( within ( bucket ) . queryByText ( / E C E 4 6 3 / ) ) . not . toBeInTheDocument ( ) ;
359+
360+ // Simulate rerender
361+ cleanup ( ) ;
362+ render ( < Maker /> ) ;
363+
364+ const newBucket = screen . getByTestId ( 'bucket' ) ;
365+ const newGrid = screen . getByTestId ( 'grid' ) ;
366+ expect ( within ( newBucket ) . getByText ( / E C E 4 6 3 / ) ) . toBeInTheDocument ( ) ;
367+
368+ expect ( screen . getByText ( / T h i s e x i s t \? \? / ) ) . toBeInTheDocument ( ) ;
369+ expect ( within ( newGrid ) . getByText ( / E C E 3 4 5 / ) ) . toBeInTheDocument ( ) ;
370+ } ) ;
335371} ) ;
0 commit comments