Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 88c367f

Browse files
authored
server: fixes cypress-io#1093, ensure github login works (cypress-io#1094)
* server: fixes cypress-io#1093, ensure github login works - add more comprehensive tests around windows and events - test that github callbacks work as expected * server: fixes failing tests * server: fixes failing tests, more resets
1 parent f0d6757 commit 88c367f

File tree

3 files changed

+87
-6
lines changed

3 files changed

+87
-6
lines changed

packages/server/lib/gui/events.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ handleEvent = (options, bus, event, id, type, arg) ->
112112
.catch(sendErr)
113113

114114
when "window:open"
115-
Windows.open(arg)
115+
Windows.open(options.projectPath, arg)
116116
.then(send)
117117
.catch(sendErr)
118118

packages/server/test/unit/gui/events_spec.coffee

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,26 @@ describe "lib/gui/events", ->
165165

166166
context "window", ->
167167
describe "window:open", ->
168-
it "calls Windows#open with args and resolves with return of Windows.open", ->
169-
@sandbox.stub(Windows, "open").withArgs({foo: "bar"}).resolves({bar: "baz"})
168+
beforeEach ->
169+
@options.projectPath = "/path/to/my/project"
170170

171-
@handleEvent("window:open", {foo: "bar"}).then (assert) =>
172-
assert.sendCalledWith({bar: "baz"})
171+
@win = @sandbox.stub({
172+
on: ->
173+
once: ->
174+
loadURL: ->
175+
webContents: {}
176+
})
177+
178+
@sandbox.stub(Windows, "create").withArgs(@options.projectPath).returns(@win)
179+
180+
it "calls Windows#open with args and resolves with return of Windows.open", ->
181+
@handleEvent("window:open", {type: "INDEX"})
182+
.then (assert) =>
183+
assert.sendCalledWith(@win)
173184

174185
it "catches errors", ->
175186
err = new Error("foo")
176-
@sandbox.stub(Windows, "open").withArgs({foo: "bar"}).rejects(err)
187+
@sandbox.stub(Windows, "open").withArgs(@options.projectPath, {foo: "bar"}).rejects(err)
177188

178189
@handleEvent("window:open", {foo: "bar"}).then (assert) =>
179190
assert.sendErrCalledWith(err)

packages/server/test/unit/gui/windows_spec.coffee

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
require("../../spec_helper")
22

33
_ = require("lodash")
4+
path = require("path")
45
Promise = require("bluebird")
56
EE = require("events").EventEmitter
67
BrowserWindow = require("electron").BrowserWindow
8+
cyDesktop = require("@packages/desktop-gui")
9+
user = require("#{root}../lib/user")
710
Windows = require("#{root}../lib/gui/windows")
811
savedState = require("#{root}../lib/saved_state")
912

1013
describe "lib/gui/windows", ->
1114
beforeEach ->
15+
Windows.reset()
16+
1217
@win = new EE()
18+
@win.loadURL = @sandbox.stub()
19+
@win.destroy = @sandbox.stub()
1320
@win.getSize = @sandbox.stub().returns([1, 2])
1421
@win.getPosition = @sandbox.stub().returns([3, 4])
1522
@win.webContents = new EE()
@@ -18,6 +25,9 @@ describe "lib/gui/windows", ->
1825

1926
@sandbox.stub(Windows, "_newBrowserWindow").returns(@win)
2027

28+
afterEach ->
29+
Windows.reset()
30+
2131
context ".getBrowserAutomation", ->
2232
beforeEach ->
2333
@sandbox.stub(Windows, "automation")
@@ -37,6 +47,66 @@ describe "lib/gui/windows", ->
3747
BrowserWindow.fromWebContents.withArgs("foo").returns("bar")
3848
expect(Windows.getByWebContents("foo")).to.eq("bar")
3949

50+
context ".open", ->
51+
beforeEach ->
52+
@sandbox.stub(Windows, "create").returns(@win)
53+
54+
it "sets default options", ->
55+
options = {
56+
type: "INDEX"
57+
}
58+
59+
Windows.open("/path/to/project", options)
60+
.then (win) ->
61+
expect(options).to.deep.eq({
62+
height: 500
63+
width: 600
64+
type: "INDEX"
65+
show: true
66+
url: cyDesktop.getPathToIndex()
67+
webPreferences: {
68+
preload: path.resolve("lib", "ipc", "ipc.js")
69+
}
70+
})
71+
72+
expect(win.loadURL).to.be.calledWith(cyDesktop.getPathToIndex())
73+
74+
it "resolves with code on GITHUB_LOGIN for will-navigate", ->
75+
options = {
76+
type: "GITHUB_LOGIN"
77+
}
78+
79+
url = "https://github.com/login"
80+
url2 = "https://github.com?code=code123"
81+
82+
@sandbox.stub(user, "getLoginUrl").resolves(url)
83+
84+
@sandbox.stub(@win.webContents, "on").withArgs("will-navigate").yieldsAsync({}, url2)
85+
86+
Windows.open("/path/to/project", options)
87+
.then (code) =>
88+
expect(code).to.eq("code123")
89+
expect(options.type).eq("GITHUB_LOGIN")
90+
expect(@win.loadURL).to.be.calledWith(url)
91+
92+
it "resolves with code on GITHUB_LOGIN for did-get-redirect-request", ->
93+
options = {
94+
type: "GITHUB_LOGIN"
95+
}
96+
97+
url = "https://github.com/login"
98+
url2 = "https://github.com?code=code123"
99+
100+
@sandbox.stub(user, "getLoginUrl").resolves(url)
101+
102+
@sandbox.stub(@win.webContents, "on").withArgs("did-get-redirect-request").yieldsAsync({}, "foo", url2)
103+
104+
Windows.open("/path/to/project", options)
105+
.then (code) =>
106+
expect(code).to.eq("code123")
107+
expect(options.type).eq("GITHUB_LOGIN")
108+
expect(@win.loadURL).to.be.calledWith(url)
109+
40110
context ".create", ->
41111
it "opens dev tools if saved state is open", ->
42112
Windows.create("/foo/", {devTools: true})

0 commit comments

Comments
 (0)