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 sinon from 'sinon'; |
| 6 | import {assert} from 'chai'; |
| 7 | |
| 8 | import {store, stateUpdated, resetState} from 'reducers/base.js'; |
| 9 | import {prpcClient} from 'prpc-client-instance.js'; |
| 10 | import * as sitewide from './sitewide.js'; |
| 11 | |
| 12 | let prpcCall; |
| 13 | |
| 14 | describe('sitewide selectors', () => { |
| 15 | beforeEach(() => { |
| 16 | store.dispatch(resetState()); |
| 17 | }); |
| 18 | it('queryParams', () => { |
| 19 | assert.deepEqual(sitewide.queryParams({}), {}); |
| 20 | assert.deepEqual(sitewide.queryParams({sitewide: {}}), {}); |
| 21 | assert.deepEqual(sitewide.queryParams({sitewide: {queryParams: |
| 22 | {q: 'owner:me'}}}), {q: 'owner:me'}); |
| 23 | }); |
| 24 | |
| 25 | describe('pageTitle', () => { |
| 26 | it('defaults to Monorail when no data', () => { |
| 27 | assert.equal(sitewide.pageTitle({}), 'Monorail'); |
| 28 | assert.equal(sitewide.pageTitle({sitewide: {}}), 'Monorail'); |
| 29 | }); |
| 30 | |
| 31 | it('uses local page title when one exists', () => { |
| 32 | assert.equal(sitewide.pageTitle( |
| 33 | {sitewide: {pageTitle: 'Issue Detail'}}), 'Issue Detail'); |
| 34 | }); |
| 35 | |
| 36 | it('shows name of viewed project', () => { |
| 37 | assert.equal(sitewide.pageTitle({ |
| 38 | sitewide: {pageTitle: 'Page'}, |
| 39 | projectV0: { |
| 40 | name: 'chromium', |
| 41 | configs: {chromium: {projectName: 'chromium'}}, |
| 42 | }, |
| 43 | }), 'Page - chromium'); |
| 44 | }); |
| 45 | }); |
| 46 | |
| 47 | describe('currentColumns', () => { |
| 48 | it('returns null no configuration', () => { |
| 49 | assert.deepEqual(sitewide.currentColumns({}), null); |
| 50 | assert.deepEqual(sitewide.currentColumns({projectV0: {}}), null); |
| 51 | const state = {projectV0: {presentationConfig: {}}}; |
| 52 | assert.deepEqual(sitewide.currentColumns(state), null); |
| 53 | }); |
| 54 | |
| 55 | it('gets columns from URL query params', () => { |
| 56 | const state = {sitewide: { |
| 57 | queryParams: {colspec: 'ID+Summary+ColumnName+Priority'}, |
| 58 | }}; |
| 59 | const expected = ['ID', 'Summary', 'ColumnName', 'Priority']; |
| 60 | assert.deepEqual(sitewide.currentColumns(state), expected); |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | describe('currentCan', () => { |
| 65 | it('uses sitewide default can by default', () => { |
| 66 | assert.deepEqual(sitewide.currentCan({}), '2'); |
| 67 | }); |
| 68 | |
| 69 | it('URL params override default can', () => { |
| 70 | assert.deepEqual(sitewide.currentCan({ |
| 71 | sitewide: { |
| 72 | queryParams: {can: '3'}, |
| 73 | }, |
| 74 | }), '3'); |
| 75 | }); |
| 76 | |
| 77 | it('undefined query param does not override default can', () => { |
| 78 | assert.deepEqual(sitewide.currentCan({ |
| 79 | sitewide: { |
| 80 | queryParams: {can: undefined}, |
| 81 | }, |
| 82 | }), '2'); |
| 83 | }); |
| 84 | }); |
| 85 | |
| 86 | describe('currentQuery', () => { |
| 87 | it('defaults to empty', () => { |
| 88 | assert.deepEqual(sitewide.currentQuery({}), ''); |
| 89 | assert.deepEqual(sitewide.currentQuery({projectV0: {}}), ''); |
| 90 | }); |
| 91 | |
| 92 | it('uses project default when no params', () => { |
| 93 | assert.deepEqual(sitewide.currentQuery({projectV0: { |
| 94 | name: 'chromium', |
| 95 | presentationConfigs: { |
| 96 | chromium: {defaultQuery: 'owner:me'}, |
| 97 | }, |
| 98 | }}), 'owner:me'); |
| 99 | }); |
| 100 | |
| 101 | it('URL query params override default query', () => { |
| 102 | assert.deepEqual(sitewide.currentQuery({ |
| 103 | projectV0: { |
| 104 | name: 'chromium', |
| 105 | presentationConfigs: { |
| 106 | chromium: {defaultQuery: 'owner:me'}, |
| 107 | }, |
| 108 | }, |
| 109 | sitewide: { |
| 110 | queryParams: {q: 'component:Infra'}, |
| 111 | }, |
| 112 | }), 'component:Infra'); |
| 113 | }); |
| 114 | |
| 115 | it('empty string in param overrides default project query', () => { |
| 116 | assert.deepEqual(sitewide.currentQuery({ |
| 117 | projectV0: { |
| 118 | name: 'chromium', |
| 119 | presentationConfigs: { |
| 120 | chromium: {defaultQuery: 'owner:me'}, |
| 121 | }, |
| 122 | }, |
| 123 | sitewide: { |
| 124 | queryParams: {q: ''}, |
| 125 | }, |
| 126 | }), ''); |
| 127 | }); |
| 128 | |
| 129 | it('undefined query param does not override default search', () => { |
| 130 | assert.deepEqual(sitewide.currentQuery({ |
| 131 | projectV0: { |
| 132 | name: 'chromium', |
| 133 | presentationConfigs: { |
| 134 | chromium: {defaultQuery: 'owner:me'}, |
| 135 | }, |
| 136 | }, |
| 137 | sitewide: { |
| 138 | queryParams: {q: undefined}, |
| 139 | }, |
| 140 | }), 'owner:me'); |
| 141 | }); |
| 142 | }); |
| 143 | }); |
| 144 | |
| 145 | |
| 146 | describe('sitewide action creators', () => { |
| 147 | beforeEach(() => { |
| 148 | prpcCall = sinon.stub(prpcClient, 'call'); |
| 149 | }); |
| 150 | |
| 151 | afterEach(() => { |
| 152 | prpcClient.call.restore(); |
| 153 | }); |
| 154 | |
| 155 | it('setQueryParams updates queryParams', async () => { |
| 156 | store.dispatch(sitewide.setQueryParams({test: 'param'})); |
| 157 | |
| 158 | await stateUpdated; |
| 159 | |
| 160 | assert.deepEqual(sitewide.queryParams(store.getState()), {test: 'param'}); |
| 161 | }); |
| 162 | |
| 163 | describe('getServerStatus', () => { |
| 164 | it('gets server status', async () => { |
| 165 | prpcCall.callsFake(() => { |
| 166 | return { |
| 167 | bannerMessage: 'Message', |
| 168 | bannerTime: 1234, |
| 169 | readOnly: true, |
| 170 | }; |
| 171 | }); |
| 172 | |
| 173 | store.dispatch(sitewide.getServerStatus()); |
| 174 | |
| 175 | await stateUpdated; |
| 176 | const state = store.getState(); |
| 177 | |
| 178 | assert.deepEqual(sitewide.bannerMessage(state), 'Message'); |
| 179 | assert.deepEqual(sitewide.bannerTime(state), 1234); |
| 180 | assert.isTrue(sitewide.readOnly(state)); |
| 181 | |
| 182 | assert.deepEqual(sitewide.requests(state), { |
| 183 | serverStatus: { |
| 184 | error: null, |
| 185 | requesting: false, |
| 186 | }, |
| 187 | }); |
| 188 | }); |
| 189 | |
| 190 | it('gets empty status', async () => { |
| 191 | prpcCall.callsFake(() => { |
| 192 | return {}; |
| 193 | }); |
| 194 | |
| 195 | store.dispatch(sitewide.getServerStatus()); |
| 196 | |
| 197 | await stateUpdated; |
| 198 | const state = store.getState(); |
| 199 | |
| 200 | assert.deepEqual(sitewide.bannerMessage(state), ''); |
| 201 | assert.deepEqual(sitewide.bannerTime(state), 0); |
| 202 | assert.isFalse(sitewide.readOnly(state)); |
| 203 | |
| 204 | assert.deepEqual(sitewide.requests(state), { |
| 205 | serverStatus: { |
| 206 | error: null, |
| 207 | requesting: false, |
| 208 | }, |
| 209 | }); |
| 210 | }); |
| 211 | |
| 212 | it('fails', async () => { |
| 213 | const error = new Error('error'); |
| 214 | prpcCall.callsFake(() => { |
| 215 | throw error; |
| 216 | }); |
| 217 | |
| 218 | store.dispatch(sitewide.getServerStatus()); |
| 219 | |
| 220 | await stateUpdated; |
| 221 | const state = store.getState(); |
| 222 | |
| 223 | assert.deepEqual(sitewide.bannerMessage(state), ''); |
| 224 | assert.deepEqual(sitewide.bannerTime(state), 0); |
| 225 | assert.isFalse(sitewide.readOnly(state)); |
| 226 | |
| 227 | assert.deepEqual(sitewide.requests(state), { |
| 228 | serverStatus: { |
| 229 | error: error, |
| 230 | requesting: false, |
| 231 | }, |
| 232 | }); |
| 233 | }); |
| 234 | }); |
| 235 | }); |