1- import React from 'react' ;
2- import { shallow } from '@edx/react-unit-test-utils' ;
3- import { Col , Row } from '@openedx/paragon' ;
1+ import { render , screen } from '@testing-library/react' ;
2+ import { IntlProvider } from 'react-intl' ;
43
54import hooks from './hooks' ;
6- import DashboardLayout , { columnConfig } from './DashboardLayout' ;
5+ import DashboardLayout from './DashboardLayout' ;
76
87jest . mock ( './hooks' , ( ) => ( {
98 useDashboardLayoutData : jest . fn ( ) ,
109} ) ) ;
1110
11+ jest . mock ( '@openedx/frontend-plugin-framework' , ( ) => ( {
12+ PluginSlot : 'PluginSlot' ,
13+ } ) ) ;
14+
15+ jest . unmock ( '@edx/frontend-platform/i18n' ) ;
16+ jest . unmock ( '@openedx/paragon' ) ;
17+ jest . unmock ( 'react' ) ;
18+
1219const hookProps = {
1320 isCollapsed : true ,
1421 sidebarShowing : false ,
1522 setSidebarShowing : jest . fn ( ) . mockName ( 'hooks.setSidebarShowing' ) ,
1623} ;
1724hooks . useDashboardLayoutData . mockReturnValue ( hookProps ) ;
1825
19- const children = ' test- children' ;
26+ const children = < div > test children</ div > ;
2027
21- let el ;
2228describe ( 'DashboardLayout' , ( ) => {
2329 beforeEach ( ( ) => {
2430 jest . clearAllMocks ( ) ;
25- el = shallow ( < DashboardLayout > { children } </ DashboardLayout > ) ;
31+ render ( < IntlProvider locale = "en" > < DashboardLayout > { children } </ DashboardLayout > </ IntlProvider > ) ;
2632 } ) ;
2733
2834 const testColumns = ( ) => {
29- it ( 'loads courseList and sidebar column layout' , ( ) => {
30- const columns = el . instance . findByType ( Row ) [ 0 ] . findByType ( Col ) ;
31- Object . keys ( columnConfig . sidebar ) . forEach ( size => {
32- expect ( columns [ 1 ] . props [ size ] ) . toEqual ( columnConfig . sidebar [ size ] ) ;
33- } ) ;
35+ it ( 'loads courseList and sidebar column layout with corresponding children' , ( ) => {
36+ const courseChildren = screen . getByText ( 'test children' ) ;
37+ const courseListCol = courseChildren . parentElement ;
38+ const sidebarCol = courseListCol . nextSibling ;
39+ expect ( courseListCol ) . toHaveClass ( 'course-list-column' ) ;
40+ expect ( sidebarCol ) . toHaveClass ( 'sidebar-column' ) ;
3441 } ) ;
3542 it ( 'displays children in first column' , ( ) => {
36- const columns = el . instance . findByType ( Row ) [ 0 ] . findByType ( Col ) ;
37- expect ( columns [ 0 ] . children ) . not . toHaveLength ( 0 ) ;
43+ const courseChildren = screen . getByText ( 'test children' ) ;
44+ const courseListCol = courseChildren . parentElement ;
45+ expect ( courseChildren ) . toBeInTheDocument ( ) ;
46+ expect ( courseListCol ) . toHaveClass ( 'course-list-column' ) ;
3847 } ) ;
3948 it ( 'displays WidgetSidebarSlot in second column' , ( ) => {
40- const columns = el . instance . findByType ( Row ) [ 0 ] . findByType ( Col ) ;
41- expect ( columns [ 1 ] . findByType ( 'WidgetSidebarSlot' ) ) . toHaveLength ( 1 ) ;
49+ const courseListCol = screen . getByText ( 'test children' ) . parentElement ;
50+ const sidebarCol = courseListCol . nextSibling ;
51+ expect ( sidebarCol ) . toHaveClass ( 'sidebar-column' ) ;
52+ expect ( sidebarCol . children [ 0 ] ) . toHaveAttribute ( 'id' , 'org.openedx.frontend.learner_dashboard.widget_sidebar.v1' ) ;
4253 } ) ;
4354 } ;
44- const testSidebarLayout = ( ) => {
55+ const testSidebarLayout = ( { isCollapsed = true } ) => {
4556 it ( 'displays withSidebar width for course list column' , ( ) => {
46- const columns = el . instance . findByType ( Row ) [ 0 ] . findByType ( Col ) ;
47- Object . keys ( columnConfig . courseList . withSidebar ) . forEach ( size => {
48- expect ( columns [ 0 ] . props [ size ] ) . toEqual ( columnConfig . courseList . withSidebar [ size ] ) ;
49- } ) ;
57+ const courseListCol = screen . getByText ( 'test children' ) . parentElement ;
58+ expect ( courseListCol ) . toHaveClass ( 'col-xl-8' ) ;
59+ const sidebarCol = courseListCol . nextSibling ;
60+ expect ( sidebarCol ) . toHaveClass ( 'sidebar-column' , ! isCollapsed && 'not-collapsed' ) ;
5061 } ) ;
5162 } ;
52- const testNoSidebarLayout = ( ) => {
63+ const testNoSidebarLayout = ( { isCollapsed = true } ) => {
5364 it ( 'displays noSidebar width for course list column' , ( ) => {
54- const columns = el . instance . findByType ( Row ) [ 0 ] . findByType ( Col ) ;
55- Object . keys ( columnConfig . courseList . noSidebar ) . forEach ( size => {
56- expect ( columns [ 0 ] . props [ size ] ) . toEqual ( columnConfig . courseList . noSidebar [ size ] ) ;
57- } ) ;
58- } ) ;
59- } ;
60- const testSnapshot = ( ) => {
61- test ( 'snapshot' , ( ) => {
62- expect ( el . snapshot ) . toMatchSnapshot ( ) ;
65+ const courseListCol = screen . getByText ( 'test children' ) . parentElement ;
66+ expect ( courseListCol ) . toHaveClass ( 'col-xl-12' ) ;
67+ const sidebarCol = courseListCol . nextSibling ;
68+ expect ( sidebarCol ) . toHaveClass ( 'sidebar-column' , ! isCollapsed && 'not-collapsed' ) ;
6369 } ) ;
6470 } ;
6571 describe ( 'collapsed' , ( ) => {
@@ -68,27 +74,18 @@ describe('DashboardLayout', () => {
6874 hooks . useDashboardLayoutData . mockReturnValueOnce ( { ...hookProps , sidebarShowing : true } ) ;
6975 } ) ;
7076 testColumns ( ) ;
71- testSnapshot ( ) ;
72- testSidebarLayout ( ) ;
77+ testSidebarLayout ( { } ) ;
7378 } ) ;
7479 describe ( 'sidebar not showing' , ( ) => {
80+ beforeEach ( ( ) => {
81+ hooks . useDashboardLayoutData . mockReturnValueOnce ( { ...hookProps } ) ;
82+ } ) ;
7583 testColumns ( ) ;
76- testSnapshot ( ) ;
77- testNoSidebarLayout ( ) ;
78- } ) ;
79- it ( 'does not show spacer component above widget sidebar' , ( ) => {
80- const columns = el . instance . findByType ( Col ) ;
81- expect ( columns [ 1 ] . findByType ( 'h2' ) . length ) . toEqual ( 0 ) ;
84+ testNoSidebarLayout ( { } ) ;
8285 } ) ;
8386 } ) ;
8487
8588 describe ( 'not collapsed' , ( ) => {
86- const testWidgetSpacing = ( ) => {
87- it ( 'shows not-collapsed class on widget sidebar' , ( ) => {
88- const columns = el . instance . findByType ( Col ) ;
89- expect ( columns [ 1 ] . props . className ) . toContain ( 'not-collapsed' ) ;
90- } ) ;
91- } ;
9289 describe ( 'sidebar showing' , ( ) => {
9390 beforeEach ( ( ) => {
9491 hooks . useDashboardLayoutData . mockReturnValueOnce ( {
@@ -98,18 +95,14 @@ describe('DashboardLayout', () => {
9895 } ) ;
9996 } ) ;
10097 testColumns ( ) ;
101- testSnapshot ( ) ;
102- testSidebarLayout ( ) ;
103- testWidgetSpacing ( ) ;
98+ testSidebarLayout ( { isCollapsed : false } ) ;
10499 } ) ;
105100 describe ( 'sidebar not showing' , ( ) => {
106101 beforeEach ( ( ) => {
107102 hooks . useDashboardLayoutData . mockReturnValueOnce ( { ...hookProps , isCollapsed : false } ) ;
108103 } ) ;
109104 testColumns ( ) ;
110- testSnapshot ( ) ;
111- testNoSidebarLayout ( ) ;
112- testWidgetSpacing ( ) ;
105+ testNoSidebarLayout ( { isCollapsed : false } ) ;
113106 } ) ;
114107 } ) ;
115108} ) ;
0 commit comments