How to use collect method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

collect_panels.js

Source: collect_panels.js Github

copy
1import sinon from 'sinon';
2import * as collectIndexPatternsDep from '../collect_index_patterns';
3import * as collectSearchSourcesDep from '../collect_search_sources';
4import { collectPanels } from '../collect_panels';
5import { expect } from 'chai';
6
7describe('collectPanels(req, dashboard)', () => {
8  let collectSearchSourcesStub;
9  let collectIndexPatternsStub;
10  let dashboard;
11
12  const savedObjectsClient = { bulkGet: sinon.mock() };
13
14  beforeEach(() => {
15    dashboard = {
16      attributes: {
17        panelsJSON: JSON.stringify([
18          { id: 'panel-01', type: 'search' },
19          { id: 'panel-02', type: 'visualization' }
20        ])
21      }
22    };
23
24    savedObjectsClient.bulkGet.returns(Promise.resolve({
25      saved_objects: [
26        { id: 'panel-01' }, { id: 'panel-02' }
27      ]
28    }));
29
30    collectIndexPatternsStub = sinon.stub(collectIndexPatternsDep, 'collectIndexPatterns');
31    collectIndexPatternsStub.returns([{ id: 'logstash-*' }]);
32    collectSearchSourcesStub = sinon.stub(collectSearchSourcesDep, 'collectSearchSources');
33    collectSearchSourcesStub.returns([ { id: 'search-01' }]);
34  });
35
36  afterEach(() => {
37    collectSearchSourcesStub.restore();
38    collectIndexPatternsStub.restore();
39    savedObjectsClient.bulkGet.reset();
40  });
41
42  it('should request each panel in the panelJSON', async () => {
43    await collectPanels(savedObjectsClient, dashboard);
44
45    expect(savedObjectsClient.bulkGet.calledOnce).to.equal(true);
46    expect(savedObjectsClient.bulkGet.getCall(0).args[0]).to.eql([{
47      id: 'panel-01',
48      type: 'search'
49    }, {
50      id: 'panel-02',
51      type: 'visualization'
52    }]);
53  });
54
55  it('should call collectSearchSources()', async () => {
56    await collectPanels(savedObjectsClient, dashboard);
57    expect(collectSearchSourcesStub.calledOnce).to.equal(true);
58    expect(collectSearchSourcesStub.args[0][1]).to.eql([
59      { id: 'panel-01' },
60      { id: 'panel-02' }
61    ]);
62  });
63
64  it('should call collectIndexPatterns()', async () => {
65    await collectPanels(savedObjectsClient, dashboard);
66
67    expect(collectIndexPatternsStub.calledOnce).to.equal(true);
68    expect(collectIndexPatternsStub.args[0][1]).to.eql([
69      { id: 'panel-01' },
70      { id: 'panel-02' }
71    ]);
72  });
73
74  it('should return panels, index patterns, search sources, and dashboard', async () => {
75    const results = await collectPanels(savedObjectsClient, dashboard);
76
77    expect(results).to.eql([
78      { id: 'panel-01' },
79      { id: 'panel-02' },
80      { id: 'logstash-*' },
81      { id: 'search-01' },
82      dashboard
83    ]);
84  });
85
86});
87
Full Screen

api.js

Source: api.js Github

copy
1'use strict';
2// api 路径
3var HOST = 'https://cnodejs.org/api/v1';
4// get /topics 主题首页
5var topics = HOST + '/topics';
6//get /topic/:id 主题详情
7var topic = HOST + '/topic';
8// post /accesstoken 验证 accessToken 的正确性
9var accesstoken = HOST + '/accesstoken';
10// post /topic_collect/collect 收藏主题
11var collect = HOST + '/topic_collect/collect';
12// post /topic_collect/de_collect 取消主题
13var de_collect = HOST + '/topic_collect/de_collect';
14// post /reply/:reply_id/ups 为评论点赞
15function reply (id) {
16  return HOST + "/reply/"+ id +"/ups"
17}
18
19// get请求方法
20function fetchGet(url, callback) {
21  // return callback(null, top250)
22  wx.request({
23    url: url,
24    header: { 'Content-Type': 'application/json' },
25    success (res) {
26      callback(null, res.data)
27    },
28    fail (e) {
29      console.error(e)
30      callback(e)
31    }
32  })
33}
34
35// post请求方法
36function fetchPost(url, data, callback) {
37  wx.request({
38    method: 'POST',
39    url: url,
40    data: data,
41    success (res) {
42      callback(null, res.data)
43    },
44    fail (e) {
45      console.error(e)
46      callback(e)
47    }
48  })
49}
50
51module.exports = {
52  // API
53  topics: topics,
54  topic: topic,
55  accesstoken: accesstoken,
56  collect: collect,
57  de_collect: de_collect,
58  reply: reply,
59
60
61  // METHOD
62  fetchGet: fetchGet,
63  fetchPost: fetchPost
64
65
66}
67
Full Screen

collector.js

Source: collector.js Github

copy
1'use strict';
2
3const collect = require('..');
4const metatests = require('metatests');
5
6metatests.test('data collector functor', test => {
7  const expectedResult = {
8    key1: 1,
9    key2: 2,
10    key3: 3,
11  };
12
13  const dc = collect(3)
14    .done((err, result) => {
15      test.error(err);
16      test.strictSame(result, expectedResult);
17      test.end();
18    })
19    .timeout(1000);
20
21  dc('key1', null, 1);
22  dc('key2', null, 2);
23  dc('key3', null, 3);
24});
25
26metatests.test('data collector method', test => {
27  const expectedResult = {
28    key1: 1,
29    key2: 2,
30    key3: 3,
31  };
32
33  const dc = collect(3)
34    .done((err, result) => {
35      test.error(err);
36      test.strictSame(result, expectedResult);
37      test.end();
38    })
39    .timeout(1000);
40
41  dc.collect('key1', null, 1);
42  dc.collect('key2', null, 2);
43  dc.collect('key3', null, 3);
44});
45
46metatests.test('data collector', test => {
47  const expectedResult = {
48    key1: 1,
49    key2: 2,
50    key3: 3,
51  };
52
53  const kc = collect(['key1', 'key2', 'key3'])
54    .done((err, result) => {
55      test.error(err);
56      test.strictSame(result, expectedResult);
57      test.end();
58    })
59    .timeout();
60
61  kc.collect('key1', null, 1);
62  kc.collect('key2', null, 2);
63  kc.collect('key3', null, 3);
64});
65
66metatests.test('distinct data collector', test => {
67  const expectedResult = {
68    key1: 2,
69    key2: 2,
70    key3: 3,
71  };
72
73  const dc = collect(3)
74    .distinct()
75    .done((err, result) => {
76      test.error(err);
77      test.strictSame(result, expectedResult);
78      test.end();
79    });
80
81  dc.pick('key1', 1);
82  dc.pick('key1', 2);
83  dc.pick('key2', 2);
84  dc.pick('key3', 3);
85});
86
87metatests.test('distinct key collector', test => {
88  const expectedResult = {
89    key1: 2,
90    key2: 2,
91    key3: 3,
92  };
93
94  const kc = collect(['key1', 'key2', 'key3'])
95    .distinct()
96    .done((err, result) => {
97      test.error(err);
98      test.strictSame(result, expectedResult);
99      test.end();
100    });
101
102  kc.pick('key1', 1);
103  kc.pick('key1', 2);
104  kc.pick('key2', 2);
105  kc.pick('key3', 3);
106});
107
108metatests.test('data collector with repeated keys', test => {
109  const dc = collect(3)
110    .timeout(100)
111    .done(err => {
112      test.assert(err);
113      test.end();
114    });
115
116  dc.collect('key1', null, 1);
117  dc.collect('key1', null, 2);
118  dc.collect('key2', null, 2);
119});
120
121metatests.test('key collector with repeated keys', test => {
122  const kc = collect(['key1', 'key2', 'key3'])
123    .timeout(100)
124    .done(err => {
125      test.assert(err);
126      test.end();
127    });
128
129  kc.collect('key1', null, 1);
130  kc.collect('key1', null, 2);
131  kc.collect('key2', null, 2);
132});
133
134metatests.test('collect with error', test => {
135  const testErr = new Error('Test error');
136  const col = collect(1);
137  col.done((err, res) => {
138    test.strictSame(err, testErr);
139    test.strictSame(res, {});
140    test.end();
141  });
142  col.fail('someKey', testErr);
143});
144
145metatests.test('collect method calling after it is done', test => {
146  const col = collect(1);
147  col.done((err, res) => {
148    test.error(err);
149    test.strictSame(res, { someKey: 'someVal' });
150    test.end();
151  });
152  col.pick('someKey', 'someVal');
153  col.pick('someKey2', 'someVal2');
154});
155
156metatests.test('keys collector receives wrong key', test => {
157  const col = collect(['rightKey']);
158  col.done((err, res) => {
159    test.error(err);
160    test.strictSame(res, { wrongKey: 'someVal', rightKey: 'someVal' });
161    test.end();
162  });
163  col.pick('wrongKey', 'someVal');
164  col.pick('rightKey', 'someVal');
165});
166
167metatests.test('distinct keys collector receives wrong key', test => {
168  const col = collect(['rightKey']).distinct();
169  col.done(err => {
170    test.assert(err);
171    test.end();
172  });
173  col.pick('wrongKey', 'someVal');
174  col.pick('rightKey', 'someVal');
175});
176
177metatests.test('collect with take', test => {
178  const col = collect(1);
179  col.done((err, res) => {
180    test.error(err);
181    test.strictSame(res, { someKey: 'someVal' });
182    test.end();
183  });
184  const af = (x, callback) => callback(null, x);
185  col.take('someKey', af, 'someVal');
186});
187
188metatests.test('collect generate callback', test => {
189  const col = collect(1);
190  col.done((err, res) => {
191    test.error(err);
192    test.strictSame(res, { someKey: 'someVal' });
193    test.end();
194  });
195  const af = (x, callback) => callback(null, x);
196  af('someVal', col.callback('someKey'));
197});
198
199metatests.test('collect generate callback shorthand', test => {
200  const col = collect(1);
201  col.done((err, res) => {
202    test.error(err);
203    test.strictSame(res, { someKey: 'someVal' });
204    test.end();
205  });
206  const af = (x, callback) => callback(null, x);
207  af('someVal', col('someKey'));
208});
209
210metatests.test('collect with timeout error', test => {
211  const timeoutErr = new Error('Collector timeout');
212  const col = collect(1)
213    .done((err, res) => {
214      test.strictSame(err, timeoutErr);
215      test.strictSame(res, {});
216      test.end();
217    })
218    .timeout(1);
219  const af = (x, callback) => setTimeout(() => callback(null, x), 2);
220  col.take('someKey', af, 'someVal');
221});
222
223metatests.test('collect with take calls bigger than expected', test => {
224  const col = collect(1).done((err, res) => {
225    test.error(err);
226    test.strictSame(Object.keys(res).length, 1);
227    test.end();
228  });
229  const af = (x, callback) => setTimeout(() => callback(null, x), 1);
230  col.take('someKey', af, 'someVal');
231  col.take('someKey2', af, 'someVal2');
232});
233
234metatests.test('cancel data collector', test => {
235  const dc = collect(3).done(err => {
236    test.assert(err);
237    test.end();
238  });
239
240  dc.pick('key', 'value');
241  dc.cancel();
242});
243
244metatests.test('cancel key collector', test => {
245  const dc = collect(['uno', 'due']).done(err => {
246    test.assert(err);
247    test.end();
248  });
249
250  dc.pick('key', 'value');
251  dc.cancel();
252});
253
254metatests.test('collect then success', test => {
255  const col = collect(1).then(
256    result => {
257      test.assert(result);
258      test.end();
259    },
260    err => {
261      test.error(err);
262      test.end();
263    }
264  );
265  col.pick('Key', 'value');
266});
267
268metatests.test('collect then fail', test => {
269  collect(5)
270    .timeout(10)
271    .then(
272      result => {
273        test.error(result);
274        test.end();
275      },
276      err => {
277        test.assert(err);
278        test.end();
279      }
280    );
281});
282
Full Screen

collect.js

Source: collect.js Github

copy
1
2const initialState = {}
3export const types = {
4    COLLECT_QUESTION_LOAD_SUCCESS:"COLLECT_QUESTION_LOAD_SUCCESS",                    
5    COLLECT_QUESTION_LOAD_ING:"COLLECT_QUESTION_LOAD_ING",                            
6    COLLECT_QUESTION_LOAD_FAIL:"COLLECT_QUESTION_LOAD_FAIL",                          
7    COLLECT_QUESTION_ADD_SUCCESS:"COLLECT_QUESTION_ADD_SUCCESS",      
8    COLLECT_QUESTION_ADD_FAIL:"COLLECT_QUESTION_ADD_FAIL",            
9    COLLECT_QUESTION_ADD_ING:"COLLECT_QUESTION_ADD_ING",       
10    COLLECT_QUESTION_DEL_SUCCESS:"COLLECT_QUESTION_DEL_SUCCESS",      
11    COLLECT_QUESTION_DEL_FAIL:"COLLECT_QUESTION_DEL_FAIL",            
12    COLLECT_QUESTION_DEL_ING:"COLLECT_QUESTION_DEL_ING",         
13
14    COLLECT_COMMODITY_LOAD_SUCCESS:"COLLECT_COMMODITY_LOAD_SUCCESS",                    
15    COLLECT_COMMODITY_LOAD_ING:"COLLECT_COMMODITY_LOAD_ING",                            
16    COLLECT_COMMODITY_LOAD_FAIL:"COLLECT_COMMODITY_LOAD_FAIL",                          
17    COLLECT_COMMODITY_ADD_SUCCESS:"COLLECT_COMMODITY_ADD_SUCCESS",      
18    COLLECT_COMMODITY_ADD_FAIL:"COLLECT_COMMODITY_ADD_FAIL",            
19    COLLECT_COMMODITY_ADD_ING:"COLLECT_COMMODITY_ADD_ING",       
20    COLLECT_COMMODITY_DEL_SUCCESS:"COLLECT_COMMODITY_DEL_SUCCESS",      
21    COLLECT_COMMODITY_DEL_FAIL:"COLLECT_COMMODITY_DEL_FAIL",            
22    COLLECT_COMMODITY_DEL_ING:"COLLECT_COMMODITY_DEL_ING",    
23
24    COLLECT_COMMUNITY_LOAD_SUCCESS:"COLLECT_COMMUNITY_LOAD_SUCCESS",                    
25    COLLECT_COMMUNITY_LOAD_ING:"COLLECT_COMMUNITY_LOAD_ING",                            
26    COLLECT_COMMUNITY_LOAD_FAIL:"COLLECT_COMMUNITY_LOAD_FAIL",                          
27    COLLECT_COMMUNITY_ADD_SUCCESS:"COLLECT_COMMUNITY_ADD_SUCCESS",      
28    COLLECT_COMMUNITY_ADD_FAIL:"COLLECT_COMMUNITY_ADD_FAIL",            
29    COLLECT_COMMUNITY_ADD_ING:"COLLECT_COMMUNITY_ADD_ING",       
30    COLLECT_COMMUNITY_DEL_SUCCESS:"COLLECT_COMMUNITY_DEL_SUCCESS",      
31    COLLECT_COMMUNITY_DEL_FAIL:"COLLECT_COMMUNITY_DEL_FAIL",            
32    COLLECT_COMMUNITY_DEL_ING:"COLLECT_COMMUNITY_DEL_ING",                       
33}
34
35export default function onAction( state = initialState, action ) {
36    console.log("actionss",action)
37    switch (action.type) {
38        case types.COLLECT_QUESTION_LOAD_ING:
39            return {
40                ...state,
41                isLoading: true
42            }
43        case types.COLLECT_QUESTION_LOAD_SUCCESS:
44            return {
45                ...state,
46                questionCollects: action.sendData,
47                isLoading: false
48            }
49        case types.COLLECT_QUESTION_LOAD_FAIL:
50            return {
51                ...state,
52                isLoading: false
53            }
54        case types.COLLECT_QUESTION_ADD_ING:
55            return {
56                ...state,
57                isLoading: true
58            }
59        case types.COLLECT_QUESTION_ADD_SUCCESS:
60            return {
61                ...state,
62                isLoading: false
63            }
64        case types.COLLECT_QUESTION_ADD_FAIL:
65            return {
66                ...state,
67                isLoading: false
68            }
69        case types.COLLECT_QUESTION_DEL_ING:
70            return {
71                ...state,
72                isLoading: true
73            }
74        case types.COLLECT_QUESTION_DEL_SUCCESS:
75            return {
76                ...state,
77                isLoading: false
78            }
79        case types.COLLECT_QUESTION_DEL_FAIL:
80            return {
81                ...state,
82                isLoading: false
83            }
84        case types.COLLECT_COMMODITY_LOAD_ING:
85            return {
86                ...state,
87                isLoading: true
88            }
89        case types.COLLECT_COMMODITY_LOAD_SUCCESS:
90            return {
91                ...state,
92                commodityCollects: action.sendData,
93                isLoading: false
94            }
95        case types.COLLECT_COMMODITY_LOAD_FAIL:
96            return {
97                ...state,
98                isLoading: false
99            }
100        case types.COLLECT_COMMODITY_ADD_ING:
101            return {
102                ...state,
103                isLoading: true
104            }
105        case types.COLLECT_COMMODITY_ADD_SUCCESS:
106            return {
107                ...state,
108                isLoading: false
109            }
110        case types.COLLECT_COMMODITY_ADD_FAIL:
111            return {
112                ...state,
113                isLoading: false
114            }
115        case types.COLLECT_COMMODITY_DEL_ING:
116            return {
117                ...state,
118                isLoading: true
119            }
120        case types.COLLECT_COMMODITY_DEL_SUCCESS:
121            return {
122                ...state,
123                isLoading: false
124            }
125        case types.COLLECT_COMMODITY_DEL_FAIL:
126            return {
127                ...state,
128                isLoading: false
129            }
130        case types.COLLECT_COMMUNITY_LOAD_ING:
131            return {
132                ...state,
133                isLoading: true
134            }
135        case types.COLLECT_COMMUNITY_LOAD_SUCCESS:
136            return {
137                ...state,
138                communityCollects: action.sendData,
139                isLoading: false
140            }
141        case types.COLLECT_COMMODITY_LOAD_FAIL:
142            return {
143                ...state,
144                isLoading: false
145            }
146        case types.COLLECT_COMMUNITY_ADD_ING:
147            return {
148                ...state,
149                isLoading: true
150            }
151        case types.COLLECT_COMMUNITY_ADD_SUCCESS:
152            return {
153                ...state,
154                isLoading: false
155            }
156        case types.COLLECT_COMMODITY_ADD_FAIL:
157            return {
158                ...state,
159                isLoading: false
160            }
161        case types.COLLECT_COMMUNITY_DEL_ING:
162            return {
163                ...state,
164                isLoading: true
165            }
166        case types.COLLECT_COMMUNITY_DEL_SUCCESS:
167            return {
168                ...state,
169                isLoading: false
170            }
171        case types.COLLECT_COMMODITY_DEL_FAIL:
172            return {
173                ...state,
174                isLoading: false
175            }
176        default: 
177            return state;
178    }
179}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)