-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathtest-runs.js
99 lines (90 loc) · 3.11 KB
/
test-runs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Copyright 2018 The WPT Dashboard Project. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
import { PolymerElement } from '../node_modules/@polymer/polymer/polymer-element.js';
import { TestRunsQuery, TestRunsUIQuery } from './test-runs-query.js';
import { productFromRun } from './product-info.js';
/**
* Base class for re-use of results-fetching behaviour, between
* multi-item (wpt-results) and single-test (test-file-results) views.
*/
const TestRunsQueryLoader = (superClass) =>
class extends superClass {
static get properties() {
return {
// Fetched + parsed JSON blobs for the runs
testRuns: {
type: Array,
},
nextPageToken: String,
displayedProducts: Array,
};
}
_fireTestRunsLoadEvent() {
// Dispatch testrunsload event. Components that consume the event:
// - wpt-permalinks
this.dispatchEvent(
new CustomEvent('testrunsload', {
detail: {testRuns: this.testRuns},
})
);
}
async loadRuns() {
const preloaded = this.testRuns;
const runs = [];
if (preloaded) {
runs.push(...preloaded);
}
// Fetch by products.
if ((this.productSpecs && this.productSpecs.length)
|| (this.runIds && this.runIds.length)) {
runs.push(
fetch(`/api/runs?${this.query}`)
.then(r => r.ok && r.json().then(runs => {
this.nextPageToken = r.headers && r.headers.get('wpt-next-page');
return runs;
}))
);
}
const fetches = await Promise.all(runs);
// Filter unresolved fetches and flatten any array-fetches into the array.
const nonEmpty = fetches.filter(e => e);
const flattened = nonEmpty.reduce((sum, item) => {
return sum.concat(Array.isArray(item) ? item : [item]);
}, []);
this.testRuns = flattened;
this.displayedProducts = this.testRuns.map(productFromRun);
this._fireTestRunsLoadEvent();
return flattened;
}
/**
* Fetch the next page of runs, using nextPageToken, if applicable.
*/
async loadMoreRuns() {
if (!this.nextPageToken) {
return;
}
const url = new URL('/api/runs', window.location);
url.searchParams.set('page', this.nextPageToken);
this.nextPageToken = null;
const r = await fetch(url);
if (!r.ok) {
return;
}
const runs = await r.json();
this.splice('testRuns', this.testRuns.length - 1, 0, ...runs);
this._fireTestRunsLoadEvent();
this.nextPageToken = r.headers && r.headers.get('wpt-next-page');
return runs;
}
};
class TestRunsBase extends TestRunsQueryLoader(TestRunsQuery(PolymerElement, TestRunsQuery.Computer)) {
// This is only used in tests, so we don't call window.customElements.define here.
static get is() {
return 'wpt-results-base';
}
}
class TestRunsUIBase extends TestRunsQueryLoader(TestRunsUIQuery(PolymerElement, TestRunsUIQuery.Computer)) {}
export { TestRunsQueryLoader, TestRunsBase, TestRunsUIBase };