Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit 260af28

Browse files
authored
feat: add filtering log events with type (related to #365) (#368)
* add filtering with type * use reduce, add converting to array * make anonymous
1 parent 70284ea commit 260af28

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

lib/basedriver/commands/event.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import _ from 'lodash';
2+
13
const commands = {};
24

35
/**
@@ -14,13 +16,25 @@ commands.logCustomEvent = function (vendor, event) {
1416
/**
1517
* Get the event log
1618
*
17-
* @param {string} type - currently unused but a placeholder for a future
18-
* feature where filtering of log events is allowed
19+
* @param {?string|Array<string>} type - the event type to filter with.
20+
* It returns all events if the type is not provided or empty string/array.
1921
* @returns {object} - the event history log object
2022
*/
21-
commands.getLogEvents = function (/*type*/) {
22-
// type is currently unused but may be implemented for filtering later on
23-
return this._eventHistory;
23+
commands.getLogEvents = function (type = null) {
24+
if (_.isEmpty(type)) {
25+
return this._eventHistory;
26+
}
27+
28+
if (!_.isArray(type)) {
29+
type = [type];
30+
}
31+
32+
return _.reduce(this._eventHistory, (acc, eventTimes, eventType) => {
33+
if (type.includes(eventType)) {
34+
acc[eventType] = eventTimes;
35+
}
36+
return acc;
37+
}, {});
2438
};
2539

2640
export default commands;

test/basedriver/commands/event-specs.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,56 @@ describe('logging custom events', function () {
2222
_.keys(events).should.eql(['commands', 'appiumEvent', 'myorg:myevent']);
2323
});
2424
});
25+
26+
describe('#getLogEvents', function () {
27+
it('should allow to get all events', async function () {
28+
const d = new BaseDriver();
29+
d._eventHistory.should.eql({commands: []});
30+
d._eventHistory.testCommand = ['1', '2', '3'];
31+
await d.getLogEvents().should.eql({
32+
commands: [], testCommand: ['1', '2', '3']
33+
});
34+
});
35+
36+
it('should filter with testCommand', async function () {
37+
const d = new BaseDriver();
38+
d._eventHistory.should.eql({commands: []});
39+
d._eventHistory.testCommand = ['1', '2', '3'];
40+
await d.getLogEvents('testCommand').should.eql({
41+
testCommand: ['1', '2', '3']
42+
});
43+
});
44+
45+
it('should not filter with wrong but can be a part of the event name', async function () {
46+
const d = new BaseDriver();
47+
d._eventHistory.should.eql({commands: []});
48+
d._eventHistory.testCommand = ['1', '2', '3'];
49+
await d.getLogEvents('testCommandDummy').should.eql({});
50+
});
51+
52+
it('should filter with multiple event keys', async function () {
53+
const d = new BaseDriver();
54+
d._eventHistory.should.eql({commands: []});
55+
d._eventHistory.testCommand = ['1', '2', '3'];
56+
d._eventHistory.testCommand2 = ['4', '5'];
57+
await d.getLogEvents(['testCommand', 'testCommand2']).should.eql({
58+
testCommand: ['1', '2', '3'], testCommand2: ['4', '5']
59+
});
60+
});
61+
62+
it('should filter with custom events', async function () {
63+
const d = new BaseDriver();
64+
d._eventHistory.should.eql({commands: []});
65+
d._eventHistory['custom:appiumEvent'] = ['1', '2', '3'];
66+
await d.getLogEvents(['custom:appiumEvent']).should.eql({
67+
'custom:appiumEvent': ['1', '2', '3']
68+
});
69+
});
70+
71+
it('should not filter with no existed event name', async function () {
72+
const d = new BaseDriver();
73+
d._eventHistory.should.eql({commands: []});
74+
d._eventHistory.testCommand = ['1', '2', '3'];
75+
await d.getLogEvents(['noEventName']).should.eql({});
76+
});
77+
});

0 commit comments

Comments
 (0)