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 '@edx/frontend-platform/i18n' ;
4
3
5
4
import hooks from './hooks' ;
6
- import DashboardLayout , { columnConfig } from './DashboardLayout' ;
5
+ import DashboardLayout from './DashboardLayout' ;
7
6
8
7
jest . mock ( './hooks' , ( ) => ( {
9
8
useDashboardLayoutData : jest . fn ( ) ,
10
9
} ) ) ;
11
10
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
+
12
19
const hookProps = {
13
20
isCollapsed : true ,
14
21
sidebarShowing : false ,
15
22
setSidebarShowing : jest . fn ( ) . mockName ( 'hooks.setSidebarShowing' ) ,
16
23
} ;
17
24
hooks . useDashboardLayoutData . mockReturnValue ( hookProps ) ;
18
25
19
- const children = ' test- children' ;
26
+ const children = < div > test children</ div > ;
20
27
21
- let el ;
22
28
describe ( 'DashboardLayout' , ( ) => {
23
29
beforeEach ( ( ) => {
24
30
jest . clearAllMocks ( ) ;
25
- el = shallow ( < DashboardLayout > { children } </ DashboardLayout > ) ;
31
+ render ( < IntlProvider locale = "en" > < DashboardLayout > { children } </ DashboardLayout > </ IntlProvider > ) ;
26
32
} ) ;
27
33
28
34
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' ) ;
34
41
} ) ;
35
42
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' ) ;
38
47
} ) ;
39
48
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' ) ;
42
53
} ) ;
43
54
} ;
44
- const testSidebarLayout = ( ) => {
55
+ const testSidebarLayout = ( { isCollapsed } ) => {
45
56
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' ) ;
50
61
} ) ;
51
62
} ;
52
- const testNoSidebarLayout = ( ) => {
63
+ const testNoSidebarLayout = ( { isCollapsed } ) => {
53
64
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' ) ;
63
69
} ) ;
64
70
} ;
65
71
describe ( 'collapsed' , ( ) => {
@@ -68,27 +74,18 @@ describe('DashboardLayout', () => {
68
74
hooks . useDashboardLayoutData . mockReturnValueOnce ( { ...hookProps , sidebarShowing : true } ) ;
69
75
} ) ;
70
76
testColumns ( ) ;
71
- testSnapshot ( ) ;
72
- testSidebarLayout ( ) ;
77
+ testSidebarLayout ( { isCollapsed : true } ) ;
73
78
} ) ;
74
79
describe ( 'sidebar not showing' , ( ) => {
80
+ beforeEach ( ( ) => {
81
+ hooks . useDashboardLayoutData . mockReturnValueOnce ( { ...hookProps } ) ;
82
+ } ) ;
75
83
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 ( { isCollapsed : true } ) ;
82
85
} ) ;
83
86
} ) ;
84
87
85
88
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
- } ;
92
89
describe ( 'sidebar showing' , ( ) => {
93
90
beforeEach ( ( ) => {
94
91
hooks . useDashboardLayoutData . mockReturnValueOnce ( {
@@ -98,18 +95,14 @@ describe('DashboardLayout', () => {
98
95
} ) ;
99
96
} ) ;
100
97
testColumns ( ) ;
101
- testSnapshot ( ) ;
102
- testSidebarLayout ( ) ;
103
- testWidgetSpacing ( ) ;
98
+ testSidebarLayout ( { isCollapsed : false } ) ;
104
99
} ) ;
105
100
describe ( 'sidebar not showing' , ( ) => {
106
101
beforeEach ( ( ) => {
107
102
hooks . useDashboardLayoutData . mockReturnValueOnce ( { ...hookProps , isCollapsed : false } ) ;
108
103
} ) ;
109
104
testColumns ( ) ;
110
- testSnapshot ( ) ;
111
- testNoSidebarLayout ( ) ;
112
- testWidgetSpacing ( ) ;
105
+ testNoSidebarLayout ( { isCollapsed : false } ) ;
113
106
} ) ;
114
107
} ) ;
115
108
} ) ;
0 commit comments