1
1
import React from 'react' ;
2
- import { render , screen , waitFor , fireEvent } from '@testing-library/react' ;
2
+ import {
3
+ render , screen , waitFor , fireEvent , within ,
4
+ } from '@testing-library/react' ;
3
5
4
- import ReportOverview from '..' ;
6
+ import { ReportOverview , ReportOverviewProps } from '..' ;
5
7
6
8
jest . mock ( '@/services/api' ) ;
7
9
jest . mock ( '@/services/SnackbarUtils' ) ;
8
10
9
- const defaultReportOverviewProps = {
10
- canEdit : false ,
11
- isNew : false ,
12
- templateId : '123456-uuid' ,
13
- text : 'Test Text' ,
11
+ const defaultReportOverviewProps : ReportOverviewProps = {
14
12
isPrint : false ,
13
+ canEditReportAppendix : false ,
14
+ canEditTemplateAppendix : false ,
15
+ isNewTemplate : false ,
16
+ templateId : '123456-uuid' ,
17
+ templateSpecificText : 'template text' ,
18
+ reportId : '7890-uuid' ,
19
+ reportSpecificText : 'report text' ,
15
20
} ;
16
21
17
22
describe ( 'ReportOverview' , ( ) => {
@@ -21,17 +26,46 @@ describe('ReportOverview', () => {
21
26
{ ...defaultReportOverviewProps }
22
27
/> ,
23
28
) ;
24
- expect ( await screen . findByText ( 'Test Text' ) ) . toBeInTheDocument ( ) ;
29
+ expect ( await screen . findByText ( defaultReportOverviewProps . templateSpecificText ) ) . toBeInTheDocument ( ) ;
30
+ expect ( await screen . findByText ( defaultReportOverviewProps . reportSpecificText ) ) . toBeInTheDocument ( ) ;
25
31
} ) ;
26
32
27
- test ( 'Show a fab button to edit if record is editable' , ( ) => {
28
- const { getByRole } = render (
33
+ test ( 'Show a fab button to edit report appendix if is editable' , ( ) => {
34
+ const { getByText } = render (
29
35
< ReportOverview
30
36
{ ...defaultReportOverviewProps }
31
- canEdit = { true }
37
+ canEditReportAppendix
32
38
/> ,
33
39
) ;
34
- const buttonEl = getByRole ( 'button' ) ;
40
+ const buttonEl = getByText ( 'Report Appendix' ) ;
41
+ expect ( buttonEl ) . not . toBeDisabled ( ) ;
42
+ expect ( buttonEl ) . toBeTruthy ( ) ;
43
+ } ) ;
44
+
45
+ test ( 'Show a fab button to edit template appendix if is editable' , ( ) => {
46
+ const { getByText } = render (
47
+ < ReportOverview
48
+ { ...defaultReportOverviewProps }
49
+ canEditTemplateAppendix
50
+ /> ,
51
+ ) ;
52
+ const buttonEl = getByText ( 'Template Appendix' ) ;
53
+ expect ( buttonEl ) . not . toBeDisabled ( ) ;
54
+ expect ( buttonEl ) . toBeTruthy ( ) ;
55
+ } ) ;
56
+
57
+ test ( 'Show both fab button to edit report + template appendix if is editable' , ( ) => {
58
+ const { getByText } = render (
59
+ < ReportOverview
60
+ { ...defaultReportOverviewProps }
61
+ canEditTemplateAppendix
62
+ canEditReportAppendix
63
+ /> ,
64
+ ) ;
65
+ let buttonEl = getByText ( 'Template Appendix' ) ;
66
+ expect ( buttonEl ) . not . toBeDisabled ( ) ;
67
+ expect ( buttonEl ) . toBeTruthy ( ) ;
68
+ buttonEl = getByText ( 'Report Appendix' ) ;
35
69
expect ( buttonEl ) . not . toBeDisabled ( ) ;
36
70
expect ( buttonEl ) . toBeTruthy ( ) ;
37
71
} ) ;
@@ -51,31 +85,64 @@ describe('ReportOverview', () => {
51
85
const { queryByRole, getByText } = render (
52
86
< ReportOverview
53
87
{ ...defaultReportOverviewProps }
54
- canEdit = { true }
55
- isPrint = { true }
56
- text = { 'Sir, this is a wendy\'s' }
88
+ canEditTemplateAppendix
89
+ canEditReportAppendix
90
+ isPrint
91
+ reportSpecificText = { 'Sir, this is a wendy\'s' }
92
+ templateSpecificText = { 'Madam, this is a wendy\'s' }
57
93
/> ,
58
94
) ;
59
95
await waitFor ( ( ) => { expect ( queryByRole ( 'button' ) ) . toBeNull ( ) ; } ) ;
60
96
expect ( getByText ( 'Sir, this is a wendy\'s' ) ) . toBeInTheDocument ( ) ;
97
+ expect ( getByText ( 'Madam, this is a wendy\'s' ) ) . toBeInTheDocument ( ) ;
98
+ } ) ;
99
+
100
+ test ( 'Opens a dialog to edit template appendix text when fab is clicked' , async ( ) => {
101
+ const { getByRole, queryByRole, getByText } = render (
102
+ < ReportOverview
103
+ { ...defaultReportOverviewProps }
104
+ canEditTemplateAppendix
105
+ canEditReportAppendix
106
+ /> ,
107
+ ) ;
108
+
109
+ await waitFor ( ( ) => {
110
+ expect ( queryByRole ( 'dialog' ) ) . toBeNull ( ) ;
111
+ } ) ;
112
+ const buttonEl = getByText ( 'Template Appendix' ) ;
113
+ await waitFor ( ( ) => {
114
+ fireEvent . click ( buttonEl ) ;
115
+ } ) ;
116
+ const dialogDiv = getByRole ( 'dialog' ) ;
117
+ expect ( dialogDiv ) . toBeInTheDocument ( ) ;
118
+ expect ( within ( dialogDiv ) . getByText (
119
+ defaultReportOverviewProps . templateSpecificText ,
120
+ ) ) . toBeInTheDocument ( ) ;
121
+ expect ( getByText ( 'Edit Appendix' ) ) . toBeInTheDocument ( ) ;
61
122
} ) ;
62
123
63
- test ( 'Opens a dialog to edit record when fab is clicked' , async ( ) => {
64
- const { getByRole, queryByRole } = render (
124
+ test ( 'Opens a dialog to edit report appendix text when fab is clicked' , async ( ) => {
125
+ const { getByRole, queryByRole, getByText } = render (
65
126
< ReportOverview
66
127
{ ...defaultReportOverviewProps }
67
- canEdit = { true }
128
+ canEditTemplateAppendix
129
+ canEditReportAppendix
68
130
/> ,
69
131
) ;
70
132
71
133
await waitFor ( ( ) => {
72
134
expect ( queryByRole ( 'dialog' ) ) . toBeNull ( ) ;
73
135
} ) ;
74
- const buttonEl = getByRole ( 'button ') ;
136
+ const buttonEl = getByText ( 'Report Appendix ') ;
75
137
await waitFor ( ( ) => {
76
138
fireEvent . click ( buttonEl ) ;
77
139
} ) ;
78
- expect ( getByRole ( 'dialog' ) ) . toBeInTheDocument ( ) ;
140
+ const dialogDiv = getByRole ( 'dialog' ) ;
141
+ expect ( dialogDiv ) . toBeInTheDocument ( ) ;
142
+ expect ( within ( dialogDiv ) . getByText (
143
+ defaultReportOverviewProps . reportSpecificText ,
144
+ ) ) . toBeInTheDocument ( ) ;
145
+ expect ( getByText ( 'Edit Appendix' ) ) . toBeInTheDocument ( ) ;
79
146
} ) ;
80
147
81
148
// These tests are TODO for now since the React-Quill component cannot be targetted for change
0 commit comments