blob: 5350989f4953b185b5f3cd6b22452bc116eea082 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001// Copyright 2019 The Chromium Authors
Copybara854996b2021-09-07 19:36:02 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'sinon';
6import {assert} from 'chai';
7import {MrPrefToggle} from './mr-pref-toggle.js';
8import {prpcClient} from 'prpc-client-instance.js';
9
10let element;
11
12describe('mr-pref-toggle', () => {
13 beforeEach(() => {
14 element = document.createElement('mr-pref-toggle');
15 element.label = 'Code';
16 element.title = 'Code font';
17 element.prefName = 'code_font';
18 document.body.appendChild(element);
19 sinon.stub(prpcClient, 'call').returns(Promise.resolve({}));
20 window.ga = sinon.stub();
21 });
22
23 afterEach(() => {
24 document.body.removeChild(element);
25 prpcClient.call.restore();
26 });
27
28 it('initializes', () => {
29 assert.instanceOf(element, MrPrefToggle);
30 });
31
32 it('toggling does not save when user is not logged in', async () => {
33 element.userDisplayName = undefined;
34 element.prefs = new Map([]);
35
36 await element.updateComplete;
37
38 const chopsToggle = element.shadowRoot.querySelector('chops-toggle');
39 chopsToggle.click();
40 await element.updateComplete;
41
42 sinon.assert.notCalled(prpcClient.call);
43
44 assert.isTrue(element.prefs.get('code_font'));
45 });
46
47 it('toggling to true saves result', async () => {
48 element.userDisplayName = 'test@example.com';
49 element.prefs = new Map([['code_font', false]]);
50
51 await element.updateComplete;
52
53 const chopsToggle = element.shadowRoot.querySelector('chops-toggle');
54
55 chopsToggle.click(); // Toggle it on.
56 await element.updateComplete;
57
58 sinon.assert.calledWith(
59 prpcClient.call,
60 'monorail.Users',
61 'SetUserPrefs',
62 {prefs: [{name: 'code_font', value: 'true'}]});
63
64 assert.isTrue(element.prefs.get('code_font'));
65 });
66
67 it('toggling to false saves result', async () => {
68 element.userDisplayName = 'test@example.com';
69 element.prefs = new Map([['code_font', true]]);
70
71 await element.updateComplete;
72
73 const chopsToggle = element.shadowRoot.querySelector('chops-toggle');
74
75 chopsToggle.click(); // Toggle it off.
76 await element.updateComplete;
77
78 sinon.assert.calledWith(
79 prpcClient.call,
80 'monorail.Users',
81 'SetUserPrefs',
82 {prefs: [{name: 'code_font', value: 'false'}]});
83
84 assert.isFalse(element.prefs.get('code_font'));
85 });
86});