|
| 1 | +import { Else, ElseIf, If, SwitchIf } from '@directives'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import { ERRORS, LogicErrors } from '@fixtures'; |
| 4 | + |
| 5 | +describe('SwitchIf', () => { |
| 6 | + it('should render content of If block', () => { |
| 7 | + const { getByText } = render( |
| 8 | + <SwitchIf> |
| 9 | + <If condition={true}>If Block</If> |
| 10 | + <Else>Else Block</Else> |
| 11 | + </SwitchIf>, |
| 12 | + ); |
| 13 | + expect(getByText('If Block')).toBeInTheDocument(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should render content of Else block', () => { |
| 17 | + const { getByText } = render( |
| 18 | + <SwitchIf> |
| 19 | + <If condition={false}>If Block</If> |
| 20 | + <Else>Else Block</Else> |
| 21 | + </SwitchIf>, |
| 22 | + ); |
| 23 | + expect(getByText('Else Block')).toBeInTheDocument(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should render content of ElseIf block', () => { |
| 27 | + const { getByText } = render( |
| 28 | + <SwitchIf> |
| 29 | + <If condition={false}>If Block</If> |
| 30 | + <ElseIf condition={true}>ElseIf Block</ElseIf> |
| 31 | + <Else>Else Block</Else> |
| 32 | + </SwitchIf>, |
| 33 | + ); |
| 34 | + |
| 35 | + expect(getByText('ElseIf Block')).toBeInTheDocument(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should render content of ElseIf block 2', () => { |
| 39 | + const { getByText } = render( |
| 40 | + <SwitchIf> |
| 41 | + <If condition={false}>If Block</If> |
| 42 | + <ElseIf condition={false}>ElseIf Block 1</ElseIf> |
| 43 | + <ElseIf condition={true}>ElseIf Block 2</ElseIf> |
| 44 | + <ElseIf condition={false}>ElseIf Block 3</ElseIf> |
| 45 | + <Else>Else Block</Else> |
| 46 | + </SwitchIf>, |
| 47 | + ); |
| 48 | + |
| 49 | + expect(getByText('ElseIf Block 2')).toBeInTheDocument(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should render content of ElseIf block 2, even when 3rd ElseIf block condition is true', () => { |
| 53 | + const { getByText } = render( |
| 54 | + <SwitchIf> |
| 55 | + <If condition={false}>If Block</If> |
| 56 | + <ElseIf condition={false}>ElseIf Block 1</ElseIf> |
| 57 | + <ElseIf condition={true}>ElseIf Block 2</ElseIf> |
| 58 | + <ElseIf condition={true}>ElseIf Block 3</ElseIf> |
| 59 | + <ElseIf condition={false}>ElseIf Block 4</ElseIf> |
| 60 | + <Else>Else Block</Else> |
| 61 | + </SwitchIf>, |
| 62 | + ); |
| 63 | + |
| 64 | + expect(getByText('ElseIf Block 2')).toBeInTheDocument(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should render an error if multiple If blocks are provided'", () => { |
| 68 | + const { getByText } = render( |
| 69 | + <SwitchIf> |
| 70 | + <If condition={true}>If Block</If> |
| 71 | + <If condition={true}>If Block</If> |
| 72 | + <Else>Else Block</Else> |
| 73 | + </SwitchIf>, |
| 74 | + ); |
| 75 | + |
| 76 | + expect(getByText(ERRORS[LogicErrors.InvalidIfBlockOrdinal])).toBeInTheDocument(); |
| 77 | + expect(getByText(ERRORS[LogicErrors.OnlyOneIfBlockExpected])).toBeInTheDocument(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should render an error if multiple Else blocks are provided'", () => { |
| 81 | + const { getByText } = render( |
| 82 | + <SwitchIf> |
| 83 | + <If condition={true}>If Block</If> |
| 84 | + <Else>Else Block 1</Else> |
| 85 | + <Else>Else Block 2</Else> |
| 86 | + </SwitchIf>, |
| 87 | + ); |
| 88 | + |
| 89 | + expect(getByText(ERRORS[LogicErrors.InvalidElseBlockOrdinal])).toBeInTheDocument(); |
| 90 | + expect(getByText(ERRORS[LogicErrors.OnlyOneElseBlockExpected])).toBeInTheDocument(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should render an error if no If, ElseIf or Else block is provided', () => { |
| 94 | + const { getByText } = render(<SwitchIf />); |
| 95 | + |
| 96 | + expect(getByText(ERRORS[LogicErrors.IfBlockExpected])).toBeInTheDocument(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should render an error if, only Else block is provided', () => { |
| 100 | + const { getByText } = render( |
| 101 | + <SwitchIf> |
| 102 | + <Else>Else Block</Else> |
| 103 | + </SwitchIf>, |
| 104 | + ); |
| 105 | + |
| 106 | + expect(getByText(ERRORS[LogicErrors.InvalidElseBlockOrdinal])).toBeInTheDocument(); |
| 107 | + expect(getByText(ERRORS[LogicErrors.IfBlockExpected])).toBeInTheDocument(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should render an error if, only ElseIf block is provided', () => { |
| 111 | + const { getByText } = render( |
| 112 | + <SwitchIf> |
| 113 | + <ElseIf>ElseIf Block</ElseIf> |
| 114 | + </SwitchIf>, |
| 115 | + ); |
| 116 | + |
| 117 | + expect(getByText(ERRORS[LogicErrors.InvalidElseIfBlockOrdinal])).toBeInTheDocument(); |
| 118 | + expect(getByText(ERRORS[LogicErrors.IfBlockExpected])).toBeInTheDocument(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should render an error if, If block is empty', () => { |
| 122 | + const { getByText } = render( |
| 123 | + <SwitchIf> |
| 124 | + <If condition={true} /> |
| 125 | + </SwitchIf>, |
| 126 | + ); |
| 127 | + |
| 128 | + expect(getByText(ERRORS[LogicErrors.ChildrenExpected])).toBeInTheDocument(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should render an error if, ElseIf block is empty', () => { |
| 132 | + const { getByText } = render( |
| 133 | + <SwitchIf> |
| 134 | + <If condition={false}>If Block</If> |
| 135 | + <ElseIf condition={true} /> |
| 136 | + </SwitchIf>, |
| 137 | + ); |
| 138 | + |
| 139 | + expect(getByText(ERRORS[LogicErrors.ChildrenExpected])).toBeInTheDocument(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should render an error if, Else block is empty', () => { |
| 143 | + const { getByText } = render( |
| 144 | + <SwitchIf> |
| 145 | + <If condition={false}>If Block</If> |
| 146 | + <Else /> |
| 147 | + </SwitchIf>, |
| 148 | + ); |
| 149 | + |
| 150 | + expect(getByText(ERRORS[LogicErrors.ChildrenExpected])).toBeInTheDocument(); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should render an error if, if no If block is provided', () => { |
| 154 | + const { getByText } = render( |
| 155 | + <SwitchIf> |
| 156 | + <ElseIf condition={true}>ElseIf Block</ElseIf> |
| 157 | + <Else>Else Block</Else> |
| 158 | + </SwitchIf>, |
| 159 | + ); |
| 160 | + |
| 161 | + expect(getByText(ERRORS[LogicErrors.IfBlockExpected])).toBeInTheDocument(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should render an error if, If block is in incorrect ordinal', () => { |
| 165 | + const { getByText } = render( |
| 166 | + <SwitchIf> |
| 167 | + <Else>Else Block</Else> |
| 168 | + <If condition={true}>If Block</If> |
| 169 | + </SwitchIf>, |
| 170 | + ); |
| 171 | + |
| 172 | + expect(getByText(ERRORS[LogicErrors.InvalidIfBlockOrdinal])).toBeInTheDocument(); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should render an error if, If block has a direct conditional block as a child', () => { |
| 176 | + const { getByText } = render( |
| 177 | + <SwitchIf> |
| 178 | + <If condition={true}> |
| 179 | + <If condition={true}>If Block</If> |
| 180 | + </If> |
| 181 | + <Else>Else Block</Else> |
| 182 | + </SwitchIf>, |
| 183 | + ); |
| 184 | + |
| 185 | + expect(getByText(ERRORS[LogicErrors.SwitchBlockExpected])).toBeInTheDocument(); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should render an error if, ElseIf block has a direct conditional block as a child', () => { |
| 189 | + const { getByText } = render( |
| 190 | + <SwitchIf> |
| 191 | + <If condition={false}>If block</If> |
| 192 | + <ElseIf condition={true}> |
| 193 | + <If condition={true}>ElseIf Block</If> |
| 194 | + </ElseIf> |
| 195 | + <Else>Else Block</Else> |
| 196 | + </SwitchIf>, |
| 197 | + ); |
| 198 | + |
| 199 | + expect(getByText(ERRORS[LogicErrors.SwitchBlockExpected])).toBeInTheDocument(); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should render an error if, Else block has a direct conditional block as a child', () => { |
| 203 | + const { getByText } = render( |
| 204 | + <SwitchIf> |
| 205 | + <If condition={false}>If block</If> |
| 206 | + <ElseIf condition={false}>ElseIf block</ElseIf> |
| 207 | + <Else> |
| 208 | + <Else>Else</Else> |
| 209 | + </Else> |
| 210 | + </SwitchIf>, |
| 211 | + ); |
| 212 | + |
| 213 | + expect(getByText(ERRORS[LogicErrors.SwitchBlockExpected])).toBeInTheDocument(); |
| 214 | + }); |
| 215 | + |
| 216 | + it('should render an error if, SwitchIf block has other elements as children', () => { |
| 217 | + const { getByText } = render( |
| 218 | + <SwitchIf> |
| 219 | + <If condition={false}>If block</If> |
| 220 | + <ElseIf condition={false}>ElseIf block</ElseIf> |
| 221 | + <Else>Else block</Else> |
| 222 | + <div>test</div> |
| 223 | + </SwitchIf>, |
| 224 | + ); |
| 225 | + |
| 226 | + expect(getByText(ERRORS[LogicErrors.InvalidElement])).toBeInTheDocument(); |
| 227 | + }); |
| 228 | +}); |
0 commit comments