|
| 1 | +/// <reference types="cypress"/> |
| 2 | + |
| 3 | +import { Classes } from "@blueprintjs/core"; |
| 4 | + |
| 5 | +/** |
| 6 | + * NOTE: Combos are events that are supposed to be sent from the main process |
| 7 | + * after a menu (or its associated shortcut) has been selected. |
| 8 | + * |
| 9 | + * Since we are running inside the browser (Electron testing support is coming soon, |
| 10 | + * see: https://www.cypress.io/blog/2019/09/26/testing-electron-js-applications-using-cypress-alpha-release/), |
| 11 | + * we are sending fake combo events to simulate the user selecting a menu item or pressing the associated |
| 12 | + * accelerator combo. |
| 13 | + */ |
| 14 | +describe("combo hotkeys", () => { |
| 15 | + const stubs: any = { |
| 16 | + navHistory: [] |
| 17 | + }; |
| 18 | + |
| 19 | + let caches:any; |
| 20 | + |
| 21 | + function resetSelection() { |
| 22 | + cy.window().then(win => { |
| 23 | + win.appState.winStates[0].views.forEach((view: any) => { |
| 24 | + caches = |
| 25 | + view.caches.forEach((cache: any) => { |
| 26 | + cache.reset(); |
| 27 | + }); |
| 28 | + }); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + function createStubs() { |
| 33 | + stubs.navHistory = []; |
| 34 | + |
| 35 | + cy.window().then(win => { |
| 36 | + const views = win.appState.winStates[0].views; |
| 37 | + for (let view of views) { |
| 38 | + for (let cache of view.caches) { |
| 39 | + stubs.navHistory.push(cy.spy(cache, "navHistory")); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // activate splitView mode |
| 44 | + const winState = win.appState.winStates[0]; |
| 45 | + winState.splitView = true; |
| 46 | + |
| 47 | + // stub copy/paste functions |
| 48 | + cy.stub(win.appState, 'copySelectedItemsPath').as('copySelectedItemsPath'); |
| 49 | + // stub reload view |
| 50 | + cy.stub(win.appState, 'refreshActiveView').as('refreshActiveView'); |
| 51 | + // stub first cache.openTerminal |
| 52 | + cy.stub(win.appState.winStates[0].views[0].caches[0], 'openTerminal').as('openTerminal'); |
| 53 | + // stub first view.cycleTab |
| 54 | + cy.stub(win.appState.winStates[0].views[0], 'cycleTab').as('cycleTab'); |
| 55 | + // stub first view.addCache |
| 56 | + cy.stub(win.appState.winStates[0].views[0], 'addCache').as('addCache'); |
| 57 | + // stub first view.closeTab |
| 58 | + cy.stub(win.appState.winStates[0].views[0], 'closeTab').as('closeTab'); |
| 59 | + // stub first win.toggleSplitView |
| 60 | + cy.spy(win.appState.winStates[0], 'toggleSplitViewMode').as('toggleSplitViewMode'); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + function getCaches() { |
| 65 | + cy.window().then(win => { |
| 66 | + caches = win.appState.winStates[0].views[0].caches; |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + before(() => { |
| 71 | + return cy.visit("http://127.0.0.1:8080"); |
| 72 | + }); |
| 73 | + |
| 74 | + beforeEach(() => { |
| 75 | + createStubs(); |
| 76 | + resetSelection(); |
| 77 | + getCaches(); |
| 78 | + // load files |
| 79 | + cy.CDAndList(0, "/"); |
| 80 | + cy.get("#view_0 [data-cy-path]") |
| 81 | + .invoke("val", "/") |
| 82 | + .focus() |
| 83 | + .blur(); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should not show toast message on copy path if no file selected", () => { |
| 87 | + // no selection: triggering fake combo should not show toast message |
| 88 | + cy.triggerFakeCombo("CmdOrCtrl+Shift+C"); |
| 89 | + |
| 90 | + cy.get(`.${Classes.TOAST}`) |
| 91 | + .should('not.be.visible'); |
| 92 | + |
| 93 | + cy.get('@copySelectedItemsPath') |
| 94 | + .should('be.calledWith', caches[0], false); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should copy file path to cb & show toast message if a file is selected", () => { |
| 98 | + // select first element |
| 99 | + cy.get("#view_0 [data-cy-file]:first") |
| 100 | + .click(); |
| 101 | + |
| 102 | + cy.triggerFakeCombo("CmdOrCtrl+Shift+C"); |
| 103 | + |
| 104 | + cy.get('@copySelectedItemsPath') |
| 105 | + .should('be.calledWith', caches[0], false); |
| 106 | + |
| 107 | + cy.get(`.${Classes.TOAST}`) |
| 108 | + .should('be.visible') |
| 109 | + .find('button') |
| 110 | + .click(); |
| 111 | + }); |
| 112 | + |
| 113 | + it("should not show toast message on copy filename if no file selected", () => { |
| 114 | + // no selection: triggering fake combo should not show toast message |
| 115 | + cy.triggerFakeCombo("CmdOrCtrl+Shift+N"); |
| 116 | + |
| 117 | + cy.get(`.${Classes.TOAST}`) |
| 118 | + .should('not.be.visible'); |
| 119 | + |
| 120 | + cy.get('@copySelectedItemsPath') |
| 121 | + .should('be.calledWith', caches[0], true); |
| 122 | + }); |
| 123 | + |
| 124 | + it("should copy file filename & show toast message if a file is selected", () => { |
| 125 | + // select first element |
| 126 | + cy.get("#view_0 [data-cy-file]:first") |
| 127 | + .click(); |
| 128 | + |
| 129 | + cy.triggerFakeCombo("CmdOrCtrl+Shift+N"); |
| 130 | + |
| 131 | + cy.get('@copySelectedItemsPath') |
| 132 | + .should('be.calledWith', caches[0], true); |
| 133 | + |
| 134 | + cy.get(`.${Classes.TOAST}`) |
| 135 | + .should('be.visible') |
| 136 | + .find('button') |
| 137 | + .click(); |
| 138 | + }); |
| 139 | + |
| 140 | + it("should open shortcuts dialog", () => { |
| 141 | + cy.triggerFakeCombo("CmdOrCtrl+S"); |
| 142 | + |
| 143 | + cy.get('.shortcutsDialog') |
| 144 | + .should('be.visible'); |
| 145 | + |
| 146 | + // close dialog |
| 147 | + cy.get(`.${Classes.DIALOG_FOOTER} .data-cy-close`) |
| 148 | + .click(); |
| 149 | + |
| 150 | + // wait for dialog to be closed otherwise |
| 151 | + // it could still be visible in next it() |
| 152 | + cy.get('.shortcutsDialog') |
| 153 | + .should('not.be.visible'); |
| 154 | + }); |
| 155 | + |
| 156 | + it("should open prefs dialog", () => { |
| 157 | + cy.triggerFakeCombo("CmdOrCtrl+,"); |
| 158 | + |
| 159 | + cy.get('.data-cy-prefs-dialog') |
| 160 | + .should('be.visible'); |
| 161 | + |
| 162 | + // close dialog |
| 163 | + cy.get(`.${Classes.DIALOG_FOOTER} .data-cy-close`) |
| 164 | + .click(); |
| 165 | + |
| 166 | + // wait for dialog to be closed otherwise |
| 167 | + // it could still be visible in next it() |
| 168 | + cy.get('.shortcutsDialog') |
| 169 | + .should('not.be.visible'); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should reload file view", () => { |
| 173 | + cy.triggerFakeCombo("CmdOrCtrl+R"); |
| 174 | + |
| 175 | + cy.get('@refreshActiveView') |
| 176 | + .should('be.calledOnce'); |
| 177 | + }); |
| 178 | + |
| 179 | + it("should open terminal", () => { |
| 180 | + cy.triggerFakeCombo("CmdOrCtrl+K"); |
| 181 | + |
| 182 | + cy.get('@openTerminal') |
| 183 | + .should('be.calledOnce'); |
| 184 | + }); |
| 185 | + |
| 186 | + it("should activate next tab", () => { |
| 187 | + cy.triggerFakeCombo("Ctrl+Tab"); |
| 188 | + |
| 189 | + cy.get('@cycleTab') |
| 190 | + .should('be.calledOnce') |
| 191 | + .should('be.calledWith', 1); |
| 192 | + }); |
| 193 | + |
| 194 | + it("should activate previous tab", () => { |
| 195 | + cy.triggerFakeCombo("Ctrl+Shift+Tab"); |
| 196 | + |
| 197 | + cy.get('@cycleTab') |
| 198 | + .should('be.calledOnce') |
| 199 | + .should('be.calledWith', -1); |
| 200 | + }); |
| 201 | + |
| 202 | + it("should open a new tab", () => { |
| 203 | + cy.triggerFakeCombo("CmdOrCtrl+T"); |
| 204 | + |
| 205 | + cy.get('@addCache') |
| 206 | + .should('be.calledOnce'); |
| 207 | + }); |
| 208 | + |
| 209 | + it("should close tab", () => { |
| 210 | + cy.triggerFakeCombo("CmdOrCtrl+W"); |
| 211 | + |
| 212 | + cy.get('@closeTab') |
| 213 | + .should('be.calledOnce'); |
| 214 | + }); |
| 215 | + |
| 216 | + it("should toggle split view", () => { |
| 217 | + // initial state: split view active |
| 218 | + cy.get("#view_1") |
| 219 | + .should("not.have.class", "active") |
| 220 | + .and('be.visible'); |
| 221 | + |
| 222 | + cy.get("#view_0") |
| 223 | + .should("have.class", "active") |
| 224 | + .and('be.visible'); |
| 225 | + |
| 226 | + // de-activate split view |
| 227 | + cy.triggerFakeCombo("CmdOrCtrl+Shift+Alt+V"); |
| 228 | + |
| 229 | + // check status: we should have only one call |
| 230 | + cy.get('@toggleSplitViewMode') |
| 231 | + .should('be.calledOnce'); |
| 232 | + |
| 233 | + cy.get("#view_0") |
| 234 | + .should('be.visible') |
| 235 | + .and('have.class', 'active'); |
| 236 | + |
| 237 | + cy.get("#view_1") |
| 238 | + .should('not.be.visible'); |
| 239 | + |
| 240 | + // re-activate split view |
| 241 | + cy.triggerFakeCombo("CmdOrCtrl+Shift+Alt+V"); |
| 242 | + |
| 243 | + // check status: should have two calls now |
| 244 | + cy.get('@toggleSplitViewMode') |
| 245 | + .should('be.calledTwice'); |
| 246 | + |
| 247 | + cy.get("#view_0") |
| 248 | + .should('be.visible') |
| 249 | + .and('not.have.class', 'active'); |
| 250 | + |
| 251 | + cy.get("#view_1") |
| 252 | + .should('be.visible') |
| 253 | + .and('have.class', 'active'); |
| 254 | + }); |
| 255 | +}); |
0 commit comments