Skip to content

Commit eae5094

Browse files
committedFeb 13, 2022
Cleaning up ensure refresh()
1 parent aa36919 commit eae5094

File tree

5 files changed

+87
-19
lines changed

5 files changed

+87
-19
lines changed
 

‎.mochaclicktest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"spec": "clicktests/spec.branches.js",
2+
"spec": "clicktests/spec.*.js",
33
"file": "./source/utils/logger.js",
44
"timeout": 20000,
55
"bail": true,

‎clicktests/environment.js

+77-12
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,6 @@ class Environment {
191191
file: filename,
192192
path: repoPath,
193193
});
194-
if (this.page) {
195-
await this.ensureRefresh();
196-
}
197194
}
198195

199196
// browser helpers
@@ -284,16 +281,18 @@ class Environment {
284281
async commit(commitMessage) {
285282
await this.waitForElementVisible('.files .file .btn-default');
286283
await this.insert('.staging input.form-control', commitMessage);
284+
const postCommitProm = this.setApiListener('/commit', 'POST');
287285
await this.click('.commit-btn');
288-
await this.ensureRefresh();
286+
await postCommitProm;
289287
await this.waitForElementHidden('.files .file .btn-default');
290288
}
291289

292290
async _createRef(type, name) {
293291
await this.click('.current ~ .new-ref button.showBranchingForm');
294292
await this.insert('.ref-icons.new-ref.editing input', name);
293+
const createRefProm = type === 'branch' ? this.setApiListener('/branches', 'POST') : this.setApiListener('/tags', 'POST')
295294
await this.click(`.new-ref ${type === 'branch' ? '.btn-primary' : '.btn-default'}`);
296-
await this.ensureRefresh();
295+
await createRefProm;
297296
await this.waitForElementVisible(`.ref.${type}[data-ta-name="${name}"]`);
298297
}
299298
createTag(name) {
@@ -314,22 +313,73 @@ class Environment {
314313
/* ignore */
315314
}
316315
await this.waitForElementHidden(`[data-ta-action="${action}"]:not([style*="display: none"])`);
317-
await this.ensureRefresh();
316+
await this.ensureRedraw();
318317
}
319318

320-
async refAction(ref, local, action) {
319+
async _refAction(ref, local, action, validateFunc) {
320+
if (!this[`_${action}ResponseWatcher`]) {
321+
this.page.on('response', async (response) => {
322+
const url = response.url();
323+
const method = response.request().method();
324+
325+
if (validateFunc(url, method)) {
326+
this.page.evaluate(`ungit._${action}Response = true`);
327+
}
328+
});
329+
this[`_${action}ResponseWatcher`] = true;
330+
}
321331
await this.clickOnNode(`.branch[data-ta-name="${ref}"][data-ta-local="${local}"]`);
322332
await this.click(`[data-ta-action="${action}"]:not([style*="display: none"]) .dropmask`);
323-
await this.ensureRefresh();
324333
await this._verifyRefAction(action);
334+
await this.page.waitForFunction(`ungit._${action}Response`, { polling: 250 });
335+
await this.page.evaluate(`ungit._${action}Response = undefined`);
336+
}
337+
338+
async pushRefAction(ref, local) {
339+
await this._refAction(ref, local, 'push', (url, method) => {
340+
if (method !== 'POST') {
341+
return false;
342+
}
343+
if (url.indexOf('/push') === -1 && url.indexOf('/tags') === -1 && url.indexOf('/branches') === -1) {
344+
return false;
345+
}
346+
return true;
347+
});
348+
}
349+
350+
async rebaseRefAction(ref, local) {
351+
await this._refAction(ref, local, 'rebase', (url, method) => {
352+
return method === 'POST' && url.indexOf('/rebase') >= -1;
353+
});
354+
}
355+
356+
async mergeRefAction(ref, local) {
357+
await this._refAction(ref, local, 'merge', (url, method) => {
358+
return method === 'POST' && url.indexOf('/merge') >= -1;
359+
});
325360
}
361+
326362
async moveRef(ref, targetNodeCommitTitle) {
327363
await this.clickOnNode(`.branch[data-ta-name="${ref}"]`);
364+
if (!this._isMoveResponseWatcherSet) {
365+
this.page.on('response', async (response) => {
366+
const url = response.url();
367+
if (response.request().method() !== 'POST') {
368+
return;
369+
}
370+
if (url.indexOf('/reset') === -1 && url.indexOf('/tags') === -1 && url.indexOf('/branches') === -1) {
371+
return;
372+
}
373+
this.page.evaluate(`ungit._moveEventResponded = true`)
374+
});
375+
this._isMoveResponseWatcherSet = true;
376+
}
328377
await this.click(
329378
`[data-ta-node-title="${targetNodeCommitTitle}"] [data-ta-action="move"]:not([style*="display: none"]) .dropmask`
330379
);
331-
await this.ensureRefresh();
332380
await this._verifyRefAction('move');
381+
await this.page.waitForFunction(`ungit._moveEventResponded`);
382+
await this.page.evaluate('ungit._moveEventResponded = undefined');
333383
}
334384

335385
// Stop program event propagation.
@@ -365,6 +415,21 @@ class Environment {
365415
});
366416
}
367417

418+
async ensureRedraw() {
419+
if (!this._gitlogResposneWatcher) {
420+
this.page.on('response', async (response) => {
421+
if (response.url().indexOf('/gitlog') > 0 && response.request().method() === 'GET') {
422+
this.page.evaluate('ungit._gitlogResponse = true');
423+
}
424+
});
425+
this._gitlogResposneWatcher = true;
426+
}
427+
await this.page.evaluate(`ungit._gitlogResponse = undefined`);
428+
await this.triggerProgramEvents();
429+
await this.page.waitForFunction(`ungit._gitlogResponse`, { polling: 250 });
430+
await this.page.waitForFunction(`ungit.__app.content().repository().graph._isLoadNodesFromApiRunning === false`, { polling: 250 });
431+
}
432+
368433
// ensure UI refresh is triggered with the latest information at the time of the call.
369434
async ensureRefresh() {
370435
logger.info('ensure refresh...');
@@ -421,15 +486,15 @@ class Environment {
421486
// to true. Use for detect if an API call was made and responded.
422487
setApiListener(apiPart, method, bodyMatcher = () => true) {
423488
const randomVariable = `ungit._${Math.floor(Math.random() * 500000)}`;
424-
this.page.evaluate(`${randomVariable} = undefined`);
425489
this.page.on('response', async (response) => {
426490
if (response.url().indexOf(apiPart) > -1 && response.request().method() === method) {
427491
if (bodyMatcher(await response.json())) {
428492
// reponse body matcher is matched, set the value to true
429493
this.page.evaluate(`${randomVariable} = true`);
430494
}
431495
}
432-
});
433-
return this.page.waitForFunction(`${randomVariable} === true`);
496+
}, { polling: 250 });
497+
return this.page.waitForFunction(`${randomVariable} === true`)
498+
.then(() => this.page.evaluate(`${randomVariable} = undefined`));
434499
}
435500
}

‎clicktests/spec.generic.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ describe('[GENERIC]', () => {
174174
await environment.ensureRefresh();
175175
});
176176

177-
it('Rebase', () => {
178-
return environment.refAction('testbranch', true, 'rebase');
177+
it('Rebase', async () => {
178+
await environment.rebaseRefAction('testbranch', true);
179179
});
180180

181181
it('Checkout master with double click', async () => {
@@ -189,8 +189,8 @@ describe('[GENERIC]', () => {
189189
await environment.ensureRefresh();
190190
});
191191

192-
it('Merge', () => {
193-
return environment.refAction('testbranch', true, 'merge');
192+
it('Merge', async () => {
193+
await environment.mergeRefAction('testbranch', true);
194194
});
195195

196196
it('Revert merge', async () => {

‎clicktests/spec.remotes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ describe('[REMOTES]', () => {
8686

8787
it('Should be possible to create and push a branch', async () => {
8888
await environment.createBranch('branchinclone');
89-
await environment.refAction('branchinclone', true, 'push');
89+
await environment.pushRefAction('branchinclone', true);
9090
await environment.waitForElementVisible('[data-ta-name="origin/branchinclone"]');
9191
});
9292

9393
it('Should be possible to force push a branch', async () => {
9494
await environment.moveRef('branchinclone', 'Init Commit 0');
95-
await environment.refAction('branchinclone', true, 'push');
95+
await environment.pushRefAction('branchinclone', true);
9696
await environment.waitForElementHidden('[data-ta-action="push"]:not([style*="display: none"])');
9797
});
9898

‎components/graph/graph.js

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ components.register('graph', (args) => new GraphViewModel(args.server, args.repo
1414
class GraphViewModel extends ComponentRoot {
1515
constructor(server, repoPath) {
1616
super();
17+
this._isLoadNodesFromApiRunning = false;
1718
this._markIdeologicalStamp = 0;
1819
this.repoPath = repoPath;
1920
this.limit = ko.observable(numberOfNodesPerLoad);
@@ -109,6 +110,7 @@ class GraphViewModel extends ComponentRoot {
109110
}
110111

111112
async loadNodesFromApi() {
113+
this._isLoadNodesFromApiRunning = true;
112114
ungit.logger.debug('graph.loadNodesFromApi() triggered');
113115
const nodeSize = this.nodes().length;
114116
const edges = [];
@@ -148,6 +150,7 @@ class GraphViewModel extends ComponentRoot {
148150
if (window.innerHeight - this.graphHeight() > 0 && nodeSize != this.nodes().length) {
149151
this.scrolledToEnd();
150152
}
153+
this._isLoadNodesFromApiRunning = false;
151154
ungit.logger.debug('graph.loadNodesFromApi() finished');
152155
}
153156
}

0 commit comments

Comments
 (0)
Please sign in to comment.