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 {MrMoveIssueDialog} from './mr-move-issue-hotlists-dialog.js'; |
| 7 | import {prpcClient} from 'prpc-client-instance.js'; |
| 8 | import * as example from 'shared/test/constants-hotlists.js'; |
| 9 | |
| 10 | let element; |
| 11 | let waitForPromises; |
| 12 | |
| 13 | describe('mr-move-issue-hotlists-dialog', () => { |
| 14 | beforeEach(async () => { |
| 15 | element = document.createElement('mr-move-issue-hotlists-dialog'); |
| 16 | document.body.appendChild(element); |
| 17 | |
| 18 | // We need to wait for promisees to resolve. Alone, the updateComplete |
| 19 | // returns without allowing our Promise.all to resolve. |
| 20 | waitForPromises = async () => element.updateComplete; |
| 21 | |
| 22 | element.userHotlists = [ |
| 23 | {name: 'Hotlist-1', ownerRef: {userId: 67890}}, |
| 24 | {name: 'Hotlist-2', ownerRef: {userId: 67890}}, |
| 25 | {name: 'Hotlist-3', ownerRef: {userId: 67890}}, |
| 26 | {name: example.HOTLIST.displayName, ownerRef: {userId: 67890}}, |
| 27 | ]; |
| 28 | element.user = {userId: 67890}; |
| 29 | element.issueRefs = [{localId: 22, projectName: 'test'}]; |
| 30 | element._viewedHotlist = example.HOTLIST; |
| 31 | await element.updateComplete; |
| 32 | |
| 33 | sinon.stub(prpcClient, 'call'); |
| 34 | }); |
| 35 | |
| 36 | afterEach(() => { |
| 37 | document.body.removeChild(element); |
| 38 | |
| 39 | prpcClient.call.restore(); |
| 40 | }); |
| 41 | |
| 42 | it('initializes', () => { |
| 43 | assert.instanceOf(element, MrMoveIssueDialog); |
| 44 | }); |
| 45 | |
| 46 | it('clicking a hotlist moves the issue', async () => { |
| 47 | element.open(); |
| 48 | await element.updateComplete; |
| 49 | |
| 50 | const targetHotlist =element.shadowRoot.querySelector( |
| 51 | '.hotlist[data-hotlist-name="Hotlist-2"]'); |
| 52 | assert.isNotNull(targetHotlist); |
| 53 | targetHotlist.click(); |
| 54 | await element.updateComplete; |
| 55 | |
| 56 | sinon.assert.calledWith(prpcClient.call, 'monorail.Features', |
| 57 | 'AddIssuesToHotlists', { |
| 58 | hotlistRefs: [{name: 'Hotlist-2', owner: {userId: 67890}}], |
| 59 | issueRefs: [{localId: 22, projectName: 'test'}], |
| 60 | }); |
| 61 | |
| 62 | sinon.assert.calledWith(prpcClient.call, 'monorail.Features', |
| 63 | 'RemoveIssuesFromHotlists', { |
| 64 | hotlistRefs: [{ |
| 65 | name: example.HOTLIST.displayName, |
| 66 | owner: {userId: 67890}, |
| 67 | }], |
| 68 | issueRefs: [{localId: 22, projectName: 'test'}], |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | it('dispatches event upon successfully moving', async () => { |
| 73 | element.open(); |
| 74 | const savedStub = sinon.stub(); |
| 75 | element.addEventListener('saveSuccess', savedStub); |
| 76 | sinon.stub(element, 'close'); |
| 77 | await element.updateComplete; |
| 78 | |
| 79 | const targetHotlist =element.shadowRoot.querySelector( |
| 80 | '.hotlist[data-hotlist-name="Hotlist-2"]'); |
| 81 | targetHotlist.click(); |
| 82 | |
| 83 | await waitForPromises(); |
| 84 | sinon.assert.calledOnce(savedStub); |
| 85 | sinon.assert.calledOnce(element.close); |
| 86 | }); |
| 87 | |
| 88 | it('dispatches no event upon error saving', async () => { |
| 89 | const mistakes = 'Mistakes were made'; |
| 90 | const error = new Error(mistakes); |
| 91 | prpcClient.call.returns(Promise.reject(error)); |
| 92 | const savedStub = sinon.stub(); |
| 93 | element.addEventListener('saveSuccess', savedStub); |
| 94 | element.open(); |
| 95 | await element.updateComplete; |
| 96 | |
| 97 | const targetHotlist =element.shadowRoot.querySelector( |
| 98 | '.hotlist[data-hotlist-name="Hotlist-2"]'); |
| 99 | targetHotlist.click(); |
| 100 | |
| 101 | await waitForPromises(); |
| 102 | sinon.assert.notCalled(savedStub); |
| 103 | assert.include(element.shadowRoot.innerHTML, mistakes); |
| 104 | }); |
| 105 | }); |