Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import {assert} from 'chai'; |
| 6 | import {MrChangeColumns} from './mr-change-columns.js'; |
| 7 | |
| 8 | |
| 9 | let element; |
| 10 | |
| 11 | describe('mr-change-columns', () => { |
| 12 | beforeEach(() => { |
| 13 | element = document.createElement('mr-change-columns'); |
| 14 | document.body.appendChild(element); |
| 15 | |
| 16 | element._page = sinon.stub(); |
| 17 | sinon.stub(element, '_currentPage').get(() => '/test'); |
| 18 | }); |
| 19 | |
| 20 | afterEach(() => { |
| 21 | document.body.removeChild(element); |
| 22 | }); |
| 23 | |
| 24 | it('initializes', () => { |
| 25 | assert.instanceOf(element, MrChangeColumns); |
| 26 | }); |
| 27 | |
| 28 | it('input initializes with currently set columns', async () => { |
| 29 | element.columns = ['ID', 'Summary']; |
| 30 | |
| 31 | await element.updateComplete; |
| 32 | |
| 33 | const input = element.shadowRoot.querySelector('#columnsInput'); |
| 34 | |
| 35 | assert.equal(input.value, 'ID Summary'); |
| 36 | }); |
| 37 | |
| 38 | it('editing input and saving updates columns in URL', async () => { |
| 39 | element.columns = ['ID', 'Summary']; |
| 40 | element.queryParams = {}; |
| 41 | |
| 42 | await element.updateComplete; |
| 43 | |
| 44 | const input = element.shadowRoot.querySelector('#columnsInput'); |
| 45 | input.value = 'ID Summary Owner'; |
| 46 | |
| 47 | element.save(); |
| 48 | |
| 49 | sinon.assert.calledWith(element._page, |
| 50 | '/test?colspec=ID%2BSummary%2BOwner'); |
| 51 | }); |
| 52 | |
| 53 | it('submitting form updates colspec', async () => { |
| 54 | element.columns = ['ID', 'Summary']; |
| 55 | element.queryParams = {}; |
| 56 | |
| 57 | await element.updateComplete; |
| 58 | |
| 59 | const input = element.shadowRoot.querySelector('#columnsInput'); |
| 60 | input.value = 'ID Summary Component'; |
| 61 | |
| 62 | // Note: HTMLFormElement.submit() does not fire event listeners. |
| 63 | const submitEvent = new Event('submit'); |
| 64 | sinon.spy(submitEvent, 'preventDefault'); |
| 65 | const form = element.shadowRoot.querySelector('form'); |
| 66 | form.dispatchEvent(submitEvent); |
| 67 | |
| 68 | // Preventing default is important to prevent native browser form submit |
| 69 | // from causing an additional navigation. |
| 70 | sinon.assert.calledOnce(submitEvent.preventDefault); |
| 71 | |
| 72 | sinon.assert.calledWith(element._page, |
| 73 | '/test?colspec=ID%2BSummary%2BComponent'); |
| 74 | }); |
| 75 | }); |