|
| 1 | +/** |
| 2 | + * Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +import { |
| 19 | + mdiCog, |
| 20 | + mdiGestureTap, |
| 21 | + mdiToggleSwitch |
| 22 | +} from '@mdi/js' |
| 23 | +import ViewToolbar from '@/components/cylc/ViewToolbar' |
| 24 | + |
| 25 | +describe('View Toolbar Component', () => { |
| 26 | + const mountToolbar = (groups) => { |
| 27 | + // configure a listener for toggle events |
| 28 | + const setOption = cy.spy().as('setOptionSpy') |
| 29 | + |
| 30 | + // mount 'em |
| 31 | + cy.vmount( |
| 32 | + ViewToolbar, |
| 33 | + { |
| 34 | + propsData: { groups }, |
| 35 | + listeners: { setOption } |
| 36 | + } |
| 37 | + ) |
| 38 | + // add the classes Vuetify requires |
| 39 | + cy.addVuetifyStyles(cy) |
| 40 | + |
| 41 | + return setOption |
| 42 | + } |
| 43 | + |
| 44 | + it('loads, toggles and runs callbacks ', () => { |
| 45 | + // set up a handler for callback events |
| 46 | + const callbacks = [] |
| 47 | + function myCallback () { |
| 48 | + callbacks.push(true) |
| 49 | + } |
| 50 | + |
| 51 | + // mount the toolbar with a couple of controls |
| 52 | + mountToolbar([ |
| 53 | + { |
| 54 | + title: 'Group 1', |
| 55 | + controls: [ |
| 56 | + { |
| 57 | + title: 'Toggle', |
| 58 | + icon: mdiToggleSwitch, |
| 59 | + action: 'toggle', |
| 60 | + value: true, |
| 61 | + key: 'toggle' |
| 62 | + }, |
| 63 | + { |
| 64 | + title: 'Callback', |
| 65 | + icon: mdiGestureTap, |
| 66 | + action: 'callback', |
| 67 | + callback: myCallback, |
| 68 | + key: 'callback' |
| 69 | + } |
| 70 | + ] |
| 71 | + } |
| 72 | + ]) |
| 73 | + |
| 74 | + // test all controls rendered |
| 75 | + cy |
| 76 | + .get('.group') |
| 77 | + .should('have.length', 1) |
| 78 | + .get('.control') |
| 79 | + .should('have.length', 2) |
| 80 | + .should('be.visible') |
| 81 | + |
| 82 | + // test action=toggle |
| 83 | + cy |
| 84 | + // the toggle should be blue because it's active (default true) |
| 85 | + .get('.control.toggle .v-icon') |
| 86 | + .should('have.class', 'blue--text') |
| 87 | + // clicking the toggle should emit a "setOption" event with the |
| 88 | + // controls key (toggle) and new value (false) |
| 89 | + .click({ force: true }) |
| 90 | + .get('@setOptionSpy') |
| 91 | + .should('have.been.calledWith', 'toggle', false) |
| 92 | + // it should not be grey because it's inactive (false) |
| 93 | + .get('.control.toggle .v-icon') |
| 94 | + .not('.blue--text') |
| 95 | + // click it again and it should become active again |
| 96 | + .click({ force: true }) |
| 97 | + .get('@setOptionSpy') |
| 98 | + .should('have.been.calledWith', 'toggle', true) |
| 99 | + .get('.control.toggle .v-icon') |
| 100 | + .should('have.class', 'blue--text') |
| 101 | + |
| 102 | + // test action=callback |
| 103 | + expect(callbacks).to.have.length(0) |
| 104 | + cy |
| 105 | + .get('.control.callback .v-icon') |
| 106 | + // clicking the icon should fire the callback |
| 107 | + .click({ force: true }) |
| 108 | + .then(() => { |
| 109 | + expect(callbacks).to.have.length(1) |
| 110 | + }) |
| 111 | + |
| 112 | + // TODO: visual regression test |
| 113 | + // https://github.com/cylc/cylc-ui/issues/178 |
| 114 | + }) |
| 115 | + |
| 116 | + it('enables and disables if', () => { |
| 117 | + // mount the toolbar with a couple of controls |
| 118 | + mountToolbar([ |
| 119 | + { |
| 120 | + title: 'Group 1', |
| 121 | + controls: [ |
| 122 | + { |
| 123 | + title: 'Foo', |
| 124 | + icon: mdiCog, |
| 125 | + action: 'toggle', |
| 126 | + value: true, |
| 127 | + key: 'foo', |
| 128 | + enableIf: ['bar'], |
| 129 | + disableIf: ['baz'] |
| 130 | + }, |
| 131 | + { |
| 132 | + title: 'Bar', |
| 133 | + icon: mdiCog, |
| 134 | + action: 'toggle', |
| 135 | + value: true, |
| 136 | + key: 'bar' |
| 137 | + }, |
| 138 | + { |
| 139 | + title: 'Baz', |
| 140 | + icon: mdiCog, |
| 141 | + action: 'toggle', |
| 142 | + value: false, |
| 143 | + key: 'baz' |
| 144 | + } |
| 145 | + ] |
| 146 | + } |
| 147 | + ]) |
| 148 | + |
| 149 | + cy |
| 150 | + // foo should start enabled (bar=true, baz=false) |
| 151 | + .get('.control.foo .v-icon') |
| 152 | + .not('.v-icon--disabled') |
| 153 | + |
| 154 | + // toggle bar |
| 155 | + .get('.control.bar .v-icon') |
| 156 | + .click({ force: true }) |
| 157 | + |
| 158 | + // foo should be disabled (bar=false, baz=false) |
| 159 | + .get('.control.foo .v-icon') |
| 160 | + .should('have.class', 'v-icon--disabled') |
| 161 | + |
| 162 | + // toggle bar & baz |
| 163 | + .get('.control.bar .v-icon') |
| 164 | + .click({ force: true }) |
| 165 | + .get('.control.baz .v-icon') |
| 166 | + .click({ force: true }) |
| 167 | + |
| 168 | + // foo should be disabled (bar=true, baz=true) |
| 169 | + .get('.control.foo .v-icon') |
| 170 | + .should('have.class', 'v-icon--disabled') |
| 171 | + |
| 172 | + // toggle baz |
| 173 | + .get('.control.baz .v-icon') |
| 174 | + .click({ force: true }) |
| 175 | + |
| 176 | + // foo should be enabled (bar=true, baz=false) |
| 177 | + .get('.control.foo .v-icon') |
| 178 | + .not('.v-icon--disabled') |
| 179 | + }) |
| 180 | + |
| 181 | + it('groups', () => { |
| 182 | + mountToolbar([ |
| 183 | + { |
| 184 | + title: 'Group 1', |
| 185 | + controls: [ |
| 186 | + { |
| 187 | + title: 'Foo', |
| 188 | + icon: mdiCog, |
| 189 | + action: 'toggle', |
| 190 | + value: true, |
| 191 | + key: 'foo' |
| 192 | + }, |
| 193 | + { |
| 194 | + title: 'Bar', |
| 195 | + icon: mdiCog, |
| 196 | + action: 'toggle', |
| 197 | + value: false, |
| 198 | + key: 'bar' |
| 199 | + } |
| 200 | + ] |
| 201 | + }, |
| 202 | + { |
| 203 | + title: 'Group 2', |
| 204 | + controls: [ |
| 205 | + { |
| 206 | + title: 'Baz', |
| 207 | + icon: mdiCog, |
| 208 | + action: 'toggle', |
| 209 | + value: true, |
| 210 | + key: 'baz' |
| 211 | + }, |
| 212 | + { |
| 213 | + title: 'Pub', |
| 214 | + icon: mdiCog, |
| 215 | + action: 'toggle', |
| 216 | + value: false, |
| 217 | + key: 'pub' |
| 218 | + } |
| 219 | + ] |
| 220 | + } |
| 221 | + ]) |
| 222 | + // TODO: visual regression test |
| 223 | + // https://github.com/cylc/cylc-ui/issues/178 |
| 224 | + }) |
| 225 | +}) |
0 commit comments