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 { |
| 7 | isShortlinkValid, |
| 8 | fromShortlink, |
| 9 | GoogleIssueTrackerIssue, |
| 10 | } from './federated.js'; |
| 11 | import {getSigninInstance} from 'shared/gapi-loader.js'; |
| 12 | |
| 13 | describe('isShortlinkValid', () => { |
| 14 | it('Returns true for valid links', () => { |
| 15 | assert.isTrue(isShortlinkValid('b/1')); |
| 16 | assert.isTrue(isShortlinkValid('b/12345678')); |
| 17 | }); |
| 18 | |
| 19 | it('Returns false for invalid links', () => { |
| 20 | assert.isFalse(isShortlinkValid('b')); |
| 21 | assert.isFalse(isShortlinkValid('b/')); |
| 22 | assert.isFalse(isShortlinkValid('b//123456')); |
| 23 | assert.isFalse(isShortlinkValid('b/123/123')); |
| 24 | assert.isFalse(isShortlinkValid('b123/123')); |
| 25 | assert.isFalse(isShortlinkValid('b/123a456')); |
| 26 | }); |
| 27 | }); |
| 28 | |
| 29 | describe('fromShortlink', () => { |
| 30 | it('Returns an issue class for valid links', () => { |
| 31 | assert.instanceOf(fromShortlink('b/1'), GoogleIssueTrackerIssue); |
| 32 | assert.instanceOf(fromShortlink('b/12345678'), GoogleIssueTrackerIssue); |
| 33 | }); |
| 34 | |
| 35 | it('Returns null for invalid links', () => { |
| 36 | assert.isNull(fromShortlink('b')); |
| 37 | assert.isNull(fromShortlink('b/')); |
| 38 | assert.isNull(fromShortlink('b//123456')); |
| 39 | assert.isNull(fromShortlink('b/123/123')); |
| 40 | assert.isNull(fromShortlink('b123/123')); |
| 41 | assert.isNull(fromShortlink('b/123a456')); |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | describe('GoogleIssueTrackerIssue', () => { |
| 46 | describe('constructor', () => { |
| 47 | it('Sets this.shortlink and this.issueID', () => { |
| 48 | const shortlink = 'b/1234'; |
| 49 | const issue = new GoogleIssueTrackerIssue(shortlink); |
| 50 | assert.equal(issue.shortlink, shortlink); |
| 51 | assert.equal(issue.issueID, 1234); |
| 52 | }); |
| 53 | |
| 54 | it('Throws when given an invalid shortlink.', () => { |
| 55 | assert.throws(() => { |
| 56 | new GoogleIssueTrackerIssue('b/123/123'); |
| 57 | }); |
| 58 | }); |
| 59 | }); |
| 60 | |
| 61 | describe('toURL', () => { |
| 62 | it('Returns a valid URL.', () => { |
| 63 | const issue = new GoogleIssueTrackerIssue('b/1234'); |
| 64 | assert.equal(issue.toURL(), 'https://issuetracker.google.com/issues/1234'); |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | describe('federated details', () => { |
| 69 | let signinImpl; |
| 70 | beforeEach(() => { |
| 71 | window.CS_env = {gapi_client_id: 'rutabaga'}; |
| 72 | signinImpl = { |
| 73 | init: sinon.stub(), |
| 74 | getUserProfileAsync: () => ( |
| 75 | Promise.resolve({ |
| 76 | getEmail: sinon.stub().returns('rutabaga@google.com'), |
| 77 | }) |
| 78 | ), |
| 79 | }; |
| 80 | // Preload signinImpl with a fake for testing. |
| 81 | getSigninInstance(signinImpl, true); |
| 82 | delete window.__gapiLoadPromise; |
| 83 | }); |
| 84 | |
| 85 | afterEach(() => { |
| 86 | delete window.CS_env; |
| 87 | }); |
| 88 | |
| 89 | describe('isOpen', () => { |
| 90 | it('Fails open', () => { |
| 91 | const issue = new GoogleIssueTrackerIssue('b/1234'); |
| 92 | assert.isTrue(issue.isOpen); |
| 93 | }); |
| 94 | |
| 95 | it('Is based on details.resolvedTime', () => { |
| 96 | const issue = new GoogleIssueTrackerIssue('b/1234'); |
| 97 | issue._federatedDetails = {resolvedTime: 12345}; |
| 98 | assert.isFalse(issue.isOpen); |
| 99 | |
| 100 | issue._federatedDetails = {}; |
| 101 | assert.isTrue(issue.isOpen); |
| 102 | }); |
| 103 | }); |
| 104 | |
| 105 | describe('summary', () => { |
| 106 | it('Returns null if not available', () => { |
| 107 | const issue = new GoogleIssueTrackerIssue('b/1234'); |
| 108 | assert.isNull(issue.summary); |
| 109 | }); |
| 110 | |
| 111 | it('Returns the summary if available', () => { |
| 112 | const issue = new GoogleIssueTrackerIssue('b/1234'); |
| 113 | issue._federatedDetails = {issueState: {title: 'Rutabaga title'}}; |
| 114 | assert.equal(issue.summary, 'Rutabaga title'); |
| 115 | }); |
| 116 | }); |
| 117 | |
| 118 | describe('toIssueRef', () => { |
| 119 | it('Returns an issue ref object', () => { |
| 120 | const issue = new GoogleIssueTrackerIssue('b/1234'); |
| 121 | issue._federatedDetails = { |
| 122 | resolvedTime: 12345, |
| 123 | issueState: { |
| 124 | title: 'A fedref issue title', |
| 125 | }, |
| 126 | }; |
| 127 | |
| 128 | assert.deepEqual(issue.toIssueRef(), { |
| 129 | extIdentifier: 'b/1234', |
| 130 | summary: 'A fedref issue title', |
| 131 | statusRef: {meansOpen: false}, |
| 132 | }); |
| 133 | }); |
| 134 | }); |
| 135 | }); |
| 136 | }); |