blob: 183a8d5a43f8e2a9f83a0c2cf78c5cfc1f77f750 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001// 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
5import {assert} from 'chai';
6import MrFlipper from './mr-flipper.js';
7import sinon from 'sinon';
8
9const xssiPrefix = ')]}\'';
10
11let element;
12
13describe('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});