Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 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 MrFlipper from './mr-flipper.js'; |
| 7 | import sinon from 'sinon'; |
| 8 | |
| 9 | const xssiPrefix = ')]}\''; |
| 10 | |
| 11 | let element; |
| 12 | |
| 13 | describe('mr-flipper', () => { |
| 14 | beforeEach(() => { |
| 15 | element = document.createElement('mr-flipper'); |
| 16 | document.body.appendChild(element); |
| 17 | |
| 18 | sinon.stub(window, 'fetch'); |
| 19 | |
| 20 | const response = new window.Response(`${xssiPrefix}{"message": "Ok"}`, { |
| 21 | status: 201, |
| 22 | headers: { |
| 23 | 'Content-type': 'application/json', |
| 24 | }, |
| 25 | }); |
| 26 | window.fetch.returns(Promise.resolve(response)); |
| 27 | }); |
| 28 | |
| 29 | afterEach(() => { |
| 30 | document.body.removeChild(element); |
| 31 | |
| 32 | window.fetch.restore(); |
| 33 | }); |
| 34 | |
| 35 | it('initializes', () => { |
| 36 | assert.instanceOf(element, MrFlipper); |
| 37 | }); |
| 38 | |
| 39 | it('renders links', async () => { |
| 40 | // Test DOM after properties are updated. |
| 41 | element._populateResponseData({ |
| 42 | cur_index: 4, |
| 43 | total_count: 13, |
| 44 | prev_url: 'http://prevurl/', |
| 45 | next_url: 'http://nexturl/', |
| 46 | list_url: 'http://listurl/', |
| 47 | }); |
| 48 | |
| 49 | await element.updateComplete; |
| 50 | |
| 51 | const prevUrlEl = element.shadowRoot.querySelector('a.prev-url'); |
| 52 | const nextUrlEl = element.shadowRoot.querySelector('a.next-url'); |
| 53 | const listUrlEl = element.shadowRoot.querySelector('a.list-url'); |
| 54 | const countsEl = element.shadowRoot.querySelector('div.counts'); |
| 55 | |
| 56 | assert.equal(prevUrlEl.href, 'http://prevurl/'); |
| 57 | assert.equal(nextUrlEl.href, 'http://nexturl/'); |
| 58 | assert.equal(listUrlEl.href, 'http://listurl/'); |
| 59 | assert.include(countsEl.innerText, '5 of 13'); |
| 60 | }); |
| 61 | |
| 62 | it('fetches flipper data when queryParams change', async () => { |
| 63 | await element.updateComplete; |
| 64 | |
| 65 | sinon.stub(element, 'fetchFlipperData'); |
| 66 | |
| 67 | element.queryParams = {id: 21, q: 'owner:me'}; |
| 68 | |
| 69 | sinon.assert.notCalled(element.fetchFlipperData); |
| 70 | |
| 71 | await element.updateComplete; |
| 72 | |
| 73 | sinon.assert.calledWith(element.fetchFlipperData, 'id=21&q=owner%3Ame'); |
| 74 | }); |
| 75 | }); |