|
| 1 | +/** |
| 2 | + * Copyright (c) HashiCorp, Inc. |
| 3 | + * SPDX-License-Identifier: MPL-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { module, test } from 'qunit'; |
| 7 | +import { setupRenderingTest } from 'showcase/tests/helpers'; |
| 8 | +import { render, click } from '@ember/test-helpers'; |
| 9 | +import { hbs } from 'ember-cli-htmlbars'; |
| 10 | +import sinon from 'sinon'; |
| 11 | + |
| 12 | +module('Integration | Component | hds/theme-switcher/index', function (hooks) { |
| 13 | + setupRenderingTest(hooks); |
| 14 | + |
| 15 | + hooks.beforeEach(function () { |
| 16 | + this.themingService = this.owner.lookup('service:hds-theming'); |
| 17 | + }); |
| 18 | + |
| 19 | + hooks.afterEach(function () { |
| 20 | + // Reset the theme after each test |
| 21 | + this.themingService.setTheme({ theme: undefined }); |
| 22 | + }); |
| 23 | + |
| 24 | + test('it should render the component with a CSS class that matches the component name', async function (assert) { |
| 25 | + await render(hbs`<Hds::ThemeSwitcher id="test-theme-switcher" />`); |
| 26 | + assert.dom('#test-theme-switcher').hasClass('hds-theme-switcher-control'); |
| 27 | + }); |
| 28 | + |
| 29 | + // TOGGLE SIZE |
| 30 | + |
| 31 | + test('it should render with small size by default', async function (assert) { |
| 32 | + await render(hbs`<Hds::ThemeSwitcher id="test-theme-switcher" />`); |
| 33 | + assert |
| 34 | + .dom('#test-theme-switcher .hds-dropdown-toggle-button') |
| 35 | + .hasClass('hds-dropdown-toggle-button--size-small'); |
| 36 | + }); |
| 37 | + |
| 38 | + test('it should render the correct CSS size class if @toggleSize is declared', async function (assert) { |
| 39 | + await render( |
| 40 | + hbs`<Hds::ThemeSwitcher @toggleSize="medium" id="test-theme-switcher" />`, |
| 41 | + ); |
| 42 | + assert |
| 43 | + .dom('#test-theme-switcher .hds-dropdown-toggle-button') |
| 44 | + .hasClass('hds-dropdown-toggle-button--size-medium'); |
| 45 | + }); |
| 46 | + |
| 47 | + // TOGGLE IS FULL WIDTH |
| 48 | + |
| 49 | + test('it should not be full width by default', async function (assert) { |
| 50 | + await render(hbs`<Hds::ThemeSwitcher id="test-theme-switcher" />`); |
| 51 | + assert |
| 52 | + .dom('#test-theme-switcher .hds-dropdown-toggle-button') |
| 53 | + .doesNotHaveClass('hds-button--width-full'); |
| 54 | + }); |
| 55 | + |
| 56 | + test('it should render full width if @toggleIsFullWidth is true', async function (assert) { |
| 57 | + await render( |
| 58 | + hbs`<Hds::ThemeSwitcher @toggleIsFullWidth={{true}} id="test-theme-switcher" />`, |
| 59 | + ); |
| 60 | + assert |
| 61 | + .dom('#test-theme-switcher .hds-dropdown-toggle-button') |
| 62 | + .hasClass('hds-dropdown-toggle-button--width-full'); |
| 63 | + }); |
| 64 | + |
| 65 | + // THEME DISPLAY |
| 66 | + |
| 67 | + test('it should display "Theme" label when no theme is set', async function (assert) { |
| 68 | + await render(hbs`<Hds::ThemeSwitcher id="test-theme-switcher" />`); |
| 69 | + assert |
| 70 | + .dom('#test-theme-switcher .hds-dropdown-toggle-button') |
| 71 | + .containsText('Theme'); |
| 72 | + }); |
| 73 | + |
| 74 | + test('it should display "System" label when system theme is set', async function (assert) { |
| 75 | + this.themingService.setTheme({ theme: 'system' }); |
| 76 | + await render(hbs`<Hds::ThemeSwitcher id="test-theme-switcher" />`); |
| 77 | + assert |
| 78 | + .dom('#test-theme-switcher .hds-dropdown-toggle-button') |
| 79 | + .containsText('System'); |
| 80 | + }); |
| 81 | + |
| 82 | + test('it should display "Light" label when light theme is set', async function (assert) { |
| 83 | + this.themingService.setTheme({ theme: 'light' }); |
| 84 | + await render(hbs`<Hds::ThemeSwitcher id="test-theme-switcher" />`); |
| 85 | + assert |
| 86 | + .dom('#test-theme-switcher .hds-dropdown-toggle-button') |
| 87 | + .containsText('Light'); |
| 88 | + }); |
| 89 | + |
| 90 | + test('it should display "Dark" label when dark theme is set', async function (assert) { |
| 91 | + this.themingService.setTheme({ theme: 'dark' }); |
| 92 | + await render(hbs`<Hds::ThemeSwitcher id="test-theme-switcher" />`); |
| 93 | + assert |
| 94 | + .dom('#test-theme-switcher .hds-dropdown-toggle-button') |
| 95 | + .containsText('Dark'); |
| 96 | + }); |
| 97 | + |
| 98 | + test('it should display "Default" label when default theme is set and hasDefaultOption is true', async function (assert) { |
| 99 | + this.themingService.setTheme({ theme: 'default' }); |
| 100 | + await render( |
| 101 | + hbs`<Hds::ThemeSwitcher @hasDefaultOption={{true}} id="test-theme-switcher" />`, |
| 102 | + ); |
| 103 | + assert |
| 104 | + .dom('#test-theme-switcher .hds-dropdown-toggle-button') |
| 105 | + .containsText('Default'); |
| 106 | + }); |
| 107 | + |
| 108 | + // HAS DEFAULT OPTION |
| 109 | + |
| 110 | + test('it should not include the "Default" option by default', async function (assert) { |
| 111 | + await render(hbs`<Hds::ThemeSwitcher id="test-theme-switcher" />`); |
| 112 | + await click('#test-theme-switcher button'); |
| 113 | + assert.dom('.hds-dropdown-list-item').exists({ count: 3 }); |
| 114 | + assert.dom('.hds-dropdown-list-item').doesNotContainText('Default'); |
| 115 | + }); |
| 116 | + |
| 117 | + test('it should include the "Default" option when `@hasDefaultOption` is `true`', async function (assert) { |
| 118 | + await render( |
| 119 | + hbs`<Hds::ThemeSwitcher @hasDefaultOption={{true}} id="test-theme-switcher" />`, |
| 120 | + ); |
| 121 | + await click('#test-theme-switcher button'); |
| 122 | + assert.dom('.hds-dropdown-list-item').exists({ count: 4 }); |
| 123 | + assert.dom('.hds-dropdown-list-item').containsText('Default'); |
| 124 | + }); |
| 125 | + |
| 126 | + // HAS SYSTEM OPTION |
| 127 | + |
| 128 | + test('it should include the "System" option by default', async function (assert) { |
| 129 | + await render(hbs`<Hds::ThemeSwitcher id="test-theme-switcher" />`); |
| 130 | + await click('#test-theme-switcher button'); |
| 131 | + assert.dom('.hds-dropdown-list-item').containsText('System'); |
| 132 | + }); |
| 133 | + |
| 134 | + test('it should not include the "System" option when `@hasSystemOption` is `false`', async function (assert) { |
| 135 | + await render( |
| 136 | + hbs`<Hds::ThemeSwitcher @hasSystemOption={{false}} id="test-theme-switcher" />`, |
| 137 | + ); |
| 138 | + await click('#test-theme-switcher button'); |
| 139 | + assert.dom('.hds-dropdown-list-item').exists({ count: 2 }); |
| 140 | + assert.dom('.hds-dropdown-list-item').doesNotContainText('System'); |
| 141 | + }); |
| 142 | + |
| 143 | + // THEME SELECTION |
| 144 | + |
| 145 | + test('it should update the theme in the service and the label in the toggle when a dropdown option is selected', async function (assert) { |
| 146 | + await render( |
| 147 | + hbs`<Hds::ThemeSwitcher @hasDefaultOption={{true}} @hasSystemOption={{true}} id="test-theme-switcher" />`, |
| 148 | + ); |
| 149 | + |
| 150 | + // open the dropdown |
| 151 | + await click('#test-theme-switcher button'); |
| 152 | + |
| 153 | + // click on `Light` theme |
| 154 | + await click('.hds-dropdown-list-item:nth-of-type(3) button'); |
| 155 | + assert.strictEqual( |
| 156 | + this.themingService.currentTheme, |
| 157 | + 'light', |
| 158 | + 'theme service should be updated to `light`', |
| 159 | + ); |
| 160 | + assert |
| 161 | + .dom('#test-theme-switcher .hds-dropdown-toggle-button') |
| 162 | + .containsText('Light'); |
| 163 | + |
| 164 | + // click on `Dark` theme |
| 165 | + await click('.hds-dropdown-list-item:nth-of-type(4) button'); |
| 166 | + assert.strictEqual( |
| 167 | + this.themingService.currentTheme, |
| 168 | + 'dark', |
| 169 | + 'theme service should be updated to `dark`', |
| 170 | + ); |
| 171 | + assert |
| 172 | + .dom('#test-theme-switcher .hds-dropdown-toggle-button') |
| 173 | + .containsText('Dark'); |
| 174 | + |
| 175 | + // click on `System` theme |
| 176 | + await click('.hds-dropdown-list-item:nth-of-type(2) button'); |
| 177 | + assert.strictEqual( |
| 178 | + this.themingService.currentTheme, |
| 179 | + 'system', |
| 180 | + 'theme service should be updated to `system`', |
| 181 | + ); |
| 182 | + assert |
| 183 | + .dom('#test-theme-switcher .hds-dropdown-toggle-button') |
| 184 | + .containsText('System'); |
| 185 | + |
| 186 | + // click on `Default` theme |
| 187 | + await click('.hds-dropdown-list-item:nth-of-type(1) button'); |
| 188 | + assert.strictEqual( |
| 189 | + this.themingService.currentTheme, |
| 190 | + 'default', |
| 191 | + 'theme service should be updated to `undefined` (default)', |
| 192 | + ); |
| 193 | + assert |
| 194 | + .dom('#test-theme-switcher .hds-dropdown-toggle-button') |
| 195 | + .containsText('Default'); |
| 196 | + }); |
| 197 | + |
| 198 | + // CALLBACKS |
| 199 | + |
| 200 | + test('it should call @onSetTheme callback when provided', async function (assert) { |
| 201 | + const onSetTheme = sinon.spy(); |
| 202 | + |
| 203 | + this.set('onSetTheme', onSetTheme); |
| 204 | + |
| 205 | + await render( |
| 206 | + hbs`<Hds::ThemeSwitcher @onSetTheme={{this.onSetTheme}} id="test-theme-switcher" />`, |
| 207 | + ); |
| 208 | + await click('#test-theme-switcher button'); |
| 209 | + |
| 210 | + // change theme |
| 211 | + const lightOptionButton = this.element.querySelector( |
| 212 | + '.hds-dropdown-list-item:nth-of-type(3) button', |
| 213 | + ); |
| 214 | + await click(lightOptionButton); |
| 215 | + |
| 216 | + assert.true(onSetTheme.calledOnce); |
| 217 | + }); |
| 218 | +}); |
0 commit comments