|
1 | 1 | import React from 'react'; |
2 | | -import { render, screen } from '@mongodb-js/testing-library-compass'; |
| 2 | +import { render, screen, userEvent } from '@mongodb-js/testing-library-compass'; |
3 | 3 | import { expect } from 'chai'; |
4 | 4 | import sinon from 'sinon'; |
5 | 5 | import { ValidationEditor } from './validation-editor'; |
6 | 6 |
|
| 7 | +function renderValidationEditor( |
| 8 | + props: Partial<React.ComponentProps<typeof ValidationEditor>> |
| 9 | +) { |
| 10 | + const validation = { |
| 11 | + validator: '', |
| 12 | + validationAction: 'warn', |
| 13 | + validationLevel: 'moderate', |
| 14 | + isChanged: false, |
| 15 | + syntaxError: null, |
| 16 | + error: null, |
| 17 | + } as const; |
| 18 | + |
| 19 | + return render( |
| 20 | + <ValidationEditor |
| 21 | + namespace="test.test" |
| 22 | + validatorChanged={() => {}} |
| 23 | + validationActionChanged={() => {}} |
| 24 | + validationLevelChanged={() => {}} |
| 25 | + cancelValidation={() => {}} |
| 26 | + saveValidation={() => {}} |
| 27 | + clearSampleDocuments={() => {}} |
| 28 | + serverVersion="8.0.5" |
| 29 | + onClickEnableEditRules={() => {}} |
| 30 | + validation={validation} |
| 31 | + isEditable |
| 32 | + isEditingEnabled |
| 33 | + {...props} |
| 34 | + /> |
| 35 | + ); |
| 36 | +} |
| 37 | + |
7 | 38 | describe('ValidationEditor [Component]', function () { |
8 | | - context('when it is an editable mode', function () { |
9 | | - const setValidatorChangedSpy = sinon.spy(); |
10 | | - const setValidationActionChangedSpy = sinon.spy(); |
11 | | - const setValidationLevelChangedSpy = sinon.spy(); |
12 | | - const setCancelValidationSpy = sinon.spy(); |
13 | | - const saveValidationSpy = sinon.spy(); |
14 | | - const clearSampleDocumentsSpy = sinon.spy(); |
15 | | - const serverVersion = '3.6.0'; |
16 | | - const validation = { |
17 | | - validator: '', |
18 | | - validationAction: 'warn', |
19 | | - validationLevel: 'moderate', |
20 | | - isChanged: false, |
21 | | - syntaxError: null, |
22 | | - error: null, |
23 | | - } as const; |
24 | | - const isEditable = true; |
| 39 | + context( |
| 40 | + 'when it is an editable mode but editing is not yet enabled', |
| 41 | + function () { |
| 42 | + let onClickEnableEditRulesSpy: sinon.SinonSpy; |
| 43 | + beforeEach(function () { |
| 44 | + onClickEnableEditRulesSpy = sinon.spy(); |
| 45 | + renderValidationEditor({ |
| 46 | + onClickEnableEditRules: onClickEnableEditRulesSpy, |
| 47 | + isEditingEnabled: false, |
| 48 | + isEditable: true, |
| 49 | + }); |
| 50 | + }); |
25 | 51 |
|
| 52 | + it('does not allow to edit the editor', function () { |
| 53 | + expect(screen.getByTestId('validation-editor')).to.exist; |
| 54 | + expect(screen.getByRole('textbox').ariaReadOnly).to.eq('true'); |
| 55 | + expect(screen.getByTestId('enable-edit-validation-button')).to.be |
| 56 | + .visible; |
| 57 | + expect(onClickEnableEditRulesSpy).to.not.have.been.called; |
| 58 | + userEvent.click(screen.getByTestId('enable-edit-validation-button')); |
| 59 | + expect(onClickEnableEditRulesSpy).to.have.been.calledOnce; |
| 60 | + }); |
| 61 | + } |
| 62 | + ); |
| 63 | + |
| 64 | + context('when it is an editable mode and editing is enabled', function () { |
26 | 65 | beforeEach(function () { |
27 | | - render( |
28 | | - <ValidationEditor |
29 | | - namespace="test.test" |
30 | | - validatorChanged={setValidatorChangedSpy} |
31 | | - validationActionChanged={setValidationActionChangedSpy} |
32 | | - validationLevelChanged={setValidationLevelChangedSpy} |
33 | | - cancelValidation={setCancelValidationSpy} |
34 | | - saveValidation={saveValidationSpy} |
35 | | - clearSampleDocuments={clearSampleDocumentsSpy} |
36 | | - serverVersion={serverVersion} |
37 | | - validation={validation} |
38 | | - isEditable={isEditable} |
39 | | - /> |
40 | | - ); |
| 66 | + renderValidationEditor({ |
| 67 | + isEditable: true, |
| 68 | + isEditingEnabled: true, |
| 69 | + }); |
41 | 70 | }); |
42 | 71 |
|
43 | 72 | it('allows to edit the editor', function () { |
44 | | - expect(screen.getByTestId('validation-editor')).to.exist; |
45 | 73 | expect(screen.getByRole('textbox').ariaReadOnly).to.eq(null); |
| 74 | + expect( |
| 75 | + screen.queryByTestId('enable-edit-validation-button') |
| 76 | + ).to.not.exist; |
46 | 77 | }); |
47 | 78 | }); |
48 | 79 |
|
49 | 80 | context('when it is a not editable mode', function () { |
50 | | - const setValidatorChangedSpy = sinon.spy(); |
51 | | - const setValidationActionChangedSpy = sinon.spy(); |
52 | | - const setValidationLevelChangedSpy = sinon.spy(); |
53 | | - const setCancelValidationSpy = sinon.spy(); |
54 | | - const saveValidationSpy = sinon.spy(); |
55 | | - const clearSampleDocumentsSpy = sinon.spy(); |
56 | | - const serverVersion = '3.6.0'; |
57 | | - const validation = { |
58 | | - validator: '', |
59 | | - validationAction: 'warn', |
60 | | - validationLevel: 'moderate', |
61 | | - isChanged: false, |
62 | | - syntaxError: null, |
63 | | - error: null, |
64 | | - } as const; |
65 | | - const isEditable = false; |
66 | | - |
67 | 81 | beforeEach(function () { |
68 | | - render( |
69 | | - <ValidationEditor |
70 | | - namespace="test.test" |
71 | | - validatorChanged={setValidatorChangedSpy} |
72 | | - validationActionChanged={setValidationActionChangedSpy} |
73 | | - validationLevelChanged={setValidationLevelChangedSpy} |
74 | | - cancelValidation={setCancelValidationSpy} |
75 | | - saveValidation={saveValidationSpy} |
76 | | - clearSampleDocuments={clearSampleDocumentsSpy} |
77 | | - serverVersion={serverVersion} |
78 | | - validation={validation} |
79 | | - isEditable={isEditable} |
80 | | - /> |
81 | | - ); |
| 82 | + renderValidationEditor({ |
| 83 | + isEditable: false, |
| 84 | + }); |
82 | 85 | }); |
83 | 86 |
|
84 | 87 | it('sets editor into readonly mode', function () { |
85 | 88 | expect(screen.getByRole('textbox').ariaReadOnly).to.eq('true'); |
| 89 | + expect( |
| 90 | + screen.queryByTestId('enable-edit-validation-button') |
| 91 | + ).to.not.exist; |
86 | 92 | }); |
87 | 93 | }); |
88 | 94 | }); |
0 commit comments