11import { pageLayoutCurrentLayoutsComponentState } from '@/page-layout/states/pageLayoutCurrentLayoutsComponentState' ;
22import { pageLayoutDraftComponentState } from '@/page-layout/states/pageLayoutDraftComponentState' ;
3+ import { activeTabIdComponentState } from '@/ui/layout/tab-list/states/activeTabIdComponentState' ;
34import { useRecoilComponentValue } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValue' ;
5+ import { useSetRecoilComponentState } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentState' ;
46import { act , renderHook } from '@testing-library/react' ;
7+ import { useSetRecoilState } from 'recoil' ;
8+ import { PageLayoutType } from '~/generated/graphql' ;
59import { useCreatePageLayoutTab } from '../useCreatePageLayoutTab' ;
610import {
7- PageLayoutTestWrapper ,
811 PAGE_LAYOUT_TEST_INSTANCE_ID ,
12+ PageLayoutTestWrapper ,
913} from './PageLayoutTestWrapper' ;
1014
1115jest . mock ( 'uuid' , ( ) => ( {
@@ -20,44 +24,49 @@ describe('useCreatePageLayoutTab', () => {
2024 it ( 'should create a new tab with default title' , ( ) => {
2125 const uuidModule = require ( 'uuid' ) ;
2226 uuidModule . v4 . mockReturnValue ( 'mock-uuid' ) ;
27+
2328 const { result } = renderHook (
2429 ( ) => ( {
2530 createTab : useCreatePageLayoutTab ( PAGE_LAYOUT_TEST_INSTANCE_ID ) ,
31+ pageLayoutDraft : useRecoilComponentValue (
32+ pageLayoutDraftComponentState ,
33+ PAGE_LAYOUT_TEST_INSTANCE_ID ,
34+ ) ,
2635 pageLayoutCurrentLayouts : useRecoilComponentValue (
2736 pageLayoutCurrentLayoutsComponentState ,
2837 PAGE_LAYOUT_TEST_INSTANCE_ID ,
2938 ) ,
30- pageLayoutDraft : useRecoilComponentValue (
31- pageLayoutDraftComponentState ,
32- PAGE_LAYOUT_TEST_INSTANCE_ID ,
39+ activeTabId : useSetRecoilState (
40+ activeTabIdComponentState . atomFamily ( {
41+ instanceId : `${ PAGE_LAYOUT_TEST_INSTANCE_ID } -tab-list` ,
42+ } ) ,
3343 ) ,
3444 } ) ,
3545 {
3646 wrapper : PageLayoutTestWrapper ,
3747 } ,
3848 ) ;
3949
40- let newTabId : string ;
4150 act ( ( ) => {
42- newTabId = result . current . createTab . createPageLayoutTab ( ) ;
51+ result . current . createTab . createPageLayoutTab ( ) ;
4352 } ) ;
4453
45- expect ( result . current . pageLayoutDraft . tabs [ 0 ] . id ) . toBe ( 'tab-mock-uuid' ) ;
54+ expect ( result . current . pageLayoutDraft . tabs ) . toHaveLength ( 1 ) ;
55+ expect ( result . current . pageLayoutDraft . tabs [ 0 ] . id ) . toBe ( 'mock-uuid' ) ;
4656 expect ( result . current . pageLayoutDraft . tabs [ 0 ] . title ) . toBe ( 'Tab 1' ) ;
4757 expect ( result . current . pageLayoutDraft . tabs [ 0 ] . position ) . toBe ( 0 ) ;
4858 expect ( result . current . pageLayoutDraft . tabs [ 0 ] . widgets ) . toEqual ( [ ] ) ;
4959
50- expect ( result . current . pageLayoutCurrentLayouts [ 'tab- mock-uuid' ] ) . toEqual ( {
60+ expect ( result . current . pageLayoutCurrentLayouts [ 'mock-uuid' ] ) . toEqual ( {
5161 desktop : [ ] ,
5262 mobile : [ ] ,
5363 } ) ;
54-
55- expect ( newTabId ! ) . toBe ( 'tab-mock-uuid' ) ;
5664 } ) ;
5765
5866 it ( 'should create a new tab with custom title' , ( ) => {
5967 const uuidModule = require ( 'uuid' ) ;
6068 uuidModule . v4 . mockReturnValue ( 'mock-uuid' ) ;
69+
6170 const { result } = renderHook (
6271 ( ) => ( {
6372 createTab : useCreatePageLayoutTab ( PAGE_LAYOUT_TEST_INSTANCE_ID ) ,
@@ -83,8 +92,9 @@ describe('useCreatePageLayoutTab', () => {
8392 it ( 'should increment position for subsequent tabs' , ( ) => {
8493 const uuidModule = require ( 'uuid' ) ;
8594 uuidModule . v4
86- . mockReturnValueOnce ( 'mock-uuid' )
95+ . mockReturnValueOnce ( 'mock-uuid-1 ' )
8796 . mockReturnValueOnce ( 'mock-uuid-2' ) ;
97+
8898 const { result } = renderHook (
8999 ( ) => ( {
90100 createTab : useCreatePageLayoutTab ( PAGE_LAYOUT_TEST_INSTANCE_ID ) ,
@@ -107,8 +117,10 @@ describe('useCreatePageLayoutTab', () => {
107117 } ) ;
108118
109119 expect ( result . current . pageLayoutDraft . tabs ) . toHaveLength ( 2 ) ;
120+ expect ( result . current . pageLayoutDraft . tabs [ 0 ] . id ) . toBe ( 'mock-uuid-1' ) ;
110121 expect ( result . current . pageLayoutDraft . tabs [ 0 ] . position ) . toBe ( 0 ) ;
111122 expect ( result . current . pageLayoutDraft . tabs [ 0 ] . title ) . toBe ( 'Tab 1' ) ;
123+ expect ( result . current . pageLayoutDraft . tabs [ 1 ] . id ) . toBe ( 'mock-uuid-2' ) ;
112124 expect ( result . current . pageLayoutDraft . tabs [ 1 ] . position ) . toBe ( 1 ) ;
113125 expect ( result . current . pageLayoutDraft . tabs [ 1 ] . title ) . toBe ( 'Tab 2' ) ;
114126 } ) ;
@@ -118,6 +130,7 @@ describe('useCreatePageLayoutTab', () => {
118130 uuidModule . v4
119131 . mockReturnValueOnce ( 'mock-uuid-1' )
120132 . mockReturnValueOnce ( 'mock-uuid-2' ) ;
133+
121134 const { result } = renderHook (
122135 ( ) => ( {
123136 createTab : useCreatePageLayoutTab ( PAGE_LAYOUT_TEST_INSTANCE_ID ) ,
@@ -131,24 +144,109 @@ describe('useCreatePageLayoutTab', () => {
131144 } ,
132145 ) ;
133146
134- let tabId1 : string = '' ;
135147 act ( ( ) => {
136- tabId1 = result . current . createTab . createPageLayoutTab ( ) ;
148+ result . current . createTab . createPageLayoutTab ( ) ;
137149 } ) ;
138150
139- let tabId2 : string = '' ;
140151 act ( ( ) => {
141- tabId2 = result . current . createTab . createPageLayoutTab ( ) ;
152+ result . current . createTab . createPageLayoutTab ( ) ;
142153 } ) ;
143154
144- expect ( result . current . pageLayoutCurrentLayouts [ tabId1 ] ) . toEqual ( {
155+ const tabIds = Object . keys ( result . current . pageLayoutCurrentLayouts ) ;
156+ expect ( tabIds ) . toHaveLength ( 2 ) ;
157+ expect ( tabIds ) . toContain ( 'mock-uuid-1' ) ;
158+ expect ( tabIds ) . toContain ( 'mock-uuid-2' ) ;
159+
160+ expect ( result . current . pageLayoutCurrentLayouts [ 'mock-uuid-1' ] ) . toEqual ( {
145161 desktop : [ ] ,
146162 mobile : [ ] ,
147163 } ) ;
148- expect ( result . current . pageLayoutCurrentLayouts [ tabId2 ] ) . toEqual ( {
164+ expect ( result . current . pageLayoutCurrentLayouts [ 'mock-uuid-2' ] ) . toEqual ( {
149165 desktop : [ ] ,
150166 mobile : [ ] ,
151167 } ) ;
152- expect ( tabId1 ) . not . toBe ( tabId2 ) ;
168+ expect ( tabIds [ 0 ] ) . not . toBe ( tabIds [ 1 ] ) ;
169+ } ) ;
170+
171+ it ( 'should set newly created tab as active' , ( ) => {
172+ const uuidModule = require ( 'uuid' ) ;
173+ uuidModule . v4 . mockReturnValue ( 'mock-uuid' ) ;
174+
175+ const { result } = renderHook (
176+ ( ) => {
177+ const getActiveTabId = useRecoilComponentValue (
178+ activeTabIdComponentState ,
179+ `${ PAGE_LAYOUT_TEST_INSTANCE_ID } -tab-list` ,
180+ ) ;
181+ return {
182+ createTab : useCreatePageLayoutTab ( PAGE_LAYOUT_TEST_INSTANCE_ID ) ,
183+ activeTabId : getActiveTabId ,
184+ } ;
185+ } ,
186+ {
187+ wrapper : PageLayoutTestWrapper ,
188+ } ,
189+ ) ;
190+
191+ expect ( result . current . activeTabId ) . toBeNull ( ) ;
192+
193+ act ( ( ) => {
194+ result . current . createTab . createPageLayoutTab ( ) ;
195+ } ) ;
196+
197+ expect ( result . current . activeTabId ) . toBe ( 'mock-uuid' ) ;
198+ } ) ;
199+
200+ it ( 'should handle creating tab when draft already has tabs' , ( ) => {
201+ const uuidModule = require ( 'uuid' ) ;
202+ uuidModule . v4 . mockReturnValue ( 'mock-uuid-new' ) ;
203+
204+ const { result } = renderHook (
205+ ( ) => {
206+ const setPageLayoutDraft = useSetRecoilComponentState (
207+ pageLayoutDraftComponentState ,
208+ PAGE_LAYOUT_TEST_INSTANCE_ID ,
209+ ) ;
210+ const pageLayoutDraft = useRecoilComponentValue (
211+ pageLayoutDraftComponentState ,
212+ PAGE_LAYOUT_TEST_INSTANCE_ID ,
213+ ) ;
214+ const createTab = useCreatePageLayoutTab ( PAGE_LAYOUT_TEST_INSTANCE_ID ) ;
215+ return { setPageLayoutDraft, pageLayoutDraft, createTab } ;
216+ } ,
217+ {
218+ wrapper : PageLayoutTestWrapper ,
219+ } ,
220+ ) ;
221+
222+ act ( ( ) => {
223+ result . current . setPageLayoutDraft ( {
224+ id : 'test-layout' ,
225+ name : 'Test Layout' ,
226+ type : PageLayoutType . DASHBOARD ,
227+ objectMetadataId : null ,
228+ tabs : [
229+ {
230+ id : 'existing-tab' ,
231+ title : 'Existing Tab' ,
232+ position : 0 ,
233+ pageLayoutId : 'test-layout' ,
234+ widgets : [ ] ,
235+ createdAt : new Date ( ) . toISOString ( ) ,
236+ updatedAt : new Date ( ) . toISOString ( ) ,
237+ deletedAt : null ,
238+ } ,
239+ ] ,
240+ } ) ;
241+ } ) ;
242+
243+ act ( ( ) => {
244+ result . current . createTab . createPageLayoutTab ( ) ;
245+ } ) ;
246+
247+ expect ( result . current . pageLayoutDraft . tabs ) . toHaveLength ( 2 ) ;
248+ expect ( result . current . pageLayoutDraft . tabs [ 1 ] . id ) . toBe ( 'mock-uuid-new' ) ;
249+ expect ( result . current . pageLayoutDraft . tabs [ 1 ] . position ) . toBe ( 1 ) ;
250+ expect ( result . current . pageLayoutDraft . tabs [ 1 ] . title ) . toBe ( 'Tab 2' ) ;
153251 } ) ;
154252} ) ;
0 commit comments