Skip to content

Commit c11e7c8

Browse files
authored
Merge pull request #26 from XbyOrange/test-sources-catch
Test sources catch
2 parents dfdeea4 + e45cc02 commit c11e7c8

File tree

4 files changed

+184
-41
lines changed

4 files changed

+184
-41
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414
## [TO BE DEPRECATED]
1515
- Last argument of Selectors will stop being assigned as "defaultValue". To define default value, it will be mandatory to pass an options object as last argument, containing a "defaultValue" property.
1616

17-
## [1.3.0] - 2019-10-10
18-
17+
## [1.3.0] - 2019-10-14
18+
- Add utility for testing catch functions of selector sources.
1919

2020
## [1.2.0] - 2019-10-14
2121
### Added

docs/selector/testing.md

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,40 @@
22

33
Selectors provides a test api for making easier developing unit tests.
44

5-
#### Testing custom queries
5+
#### Testing selector functions
66

77
```js
8-
booksCollection.addCustomQuery({
9-
myQuery: id => ({
8+
const mySelector = new Selector({
9+
booksCollection
10+
}, results => results[0]);
11+
```
12+
13+
```js
14+
expect(mySelector.test.selector(["foo"])).toEqual("foo");
15+
```
16+
17+
#### Testing selector queries
18+
19+
```js
20+
const mySelector = new Selector({
21+
source: booksCollection,
22+
query: id => {
1023
params: {
1124
id
1225
}
13-
})
14-
});
15-
26+
}
27+
}, result => result);
1628
```
1729

1830
```js
19-
expect(booksCollection.test.customQueries.myQuery("foo")).toEqual({
31+
expect(mySelector.test.queries[0]("foo")).toEqual({
2032
params: {
2133
id: "foo"
2234
}
2335
});
2436
```
2537

26-
#### Testing selector queries
38+
#### Testing selector sources "catch" methods
2739

2840
```js
2941
const mySelector = new Selector({
@@ -32,26 +44,77 @@ const mySelector = new Selector({
3244
params: {
3345
id
3446
}
35-
}
47+
},
48+
catch: err => err.message
3649
}, result => result);
3750
```
3851

3952
```js
40-
expect(mySelector.test.queries[0]("foo")).toEqual({
53+
expect(mySelector.test.catches[0](new Error("foo"))).toEqual("foo");
54+
```
55+
56+
#### Testing selector queries and catches when sources are concurrent
57+
58+
For concurrent sources, testing objects will be exposed inside an array in the same order than sources are declared.
59+
60+
```js
61+
const mySelector = new Selector(
62+
[{
63+
source: booksCollection,
64+
query: id => {
65+
params: {
66+
bookId
67+
}
68+
},
69+
catch: () => "Error retrieving books";
70+
},
71+
{
72+
source: authorsCollection,
73+
query: id => {
74+
params: {
75+
authorId
76+
}
77+
},
78+
catch: () => "Error retrieving authors";
79+
}]
80+
, result => result);
81+
```
82+
83+
```js
84+
expect(mySelector.test.queries[0][0]("foo")).toEqual({
4185
params: {
42-
id: "foo"
86+
bookId: "foo"
87+
}
88+
});
89+
90+
expect(mySelector.test.queries[0][1]("foo")).toEqual({
91+
params: {
92+
authorId: "foo"
4393
}
4494
});
95+
96+
expect(mySelector.test.catches[0][0]()).toEqual("Error retrieving books");
97+
98+
expect(mySelector.test.catches[0][1]()).toEqual("Error retrieving authors");
4599
```
46100

47-
#### Testing selector functions
101+
#### Testing custom queries
48102

49103
```js
50-
const mySelector = new Selector({
51-
booksCollection
52-
}, results => results[0]);
104+
booksCollection.addCustomQuery({
105+
myQuery: id => ({
106+
params: {
107+
id
108+
}
109+
})
110+
});
111+
53112
```
54113

55114
```js
56-
expect(mySelector.test.selector(["foo"])).toEqual("foo");
115+
expect(booksCollection.test.customQueries.myQuery("foo")).toEqual({
116+
params: {
117+
id: "foo"
118+
}
119+
});
57120
```

src/Selector.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,39 @@ export class Selector extends Origin {
3535

3636
const sourceIds = [];
3737

38-
const getTestQueries = sourcesOfLevel => {
38+
const getTestObjects = sourcesOfLevel => {
3939
const queries = [];
40+
const catches = [];
4041
sourcesOfLevel.forEach(source => {
4142
if (Array.isArray(source)) {
42-
queries.push(getTestQueries(source));
43+
const testObjects = getTestObjects(source);
44+
queries.push(testObjects.queries);
45+
catches.push(testObjects.catches);
4346
} else {
44-
const hasQuery = !!source.source;
45-
sourceIds.push(hasQuery ? source.source._id : source._id);
46-
if (hasQuery) {
47+
const isSourceObject = !!source.source;
48+
sourceIds.push(isSourceObject ? source.source._id : source._id);
49+
if (isSourceObject && source.query) {
4750
queries.push(source.query);
4851
}
52+
if (isSourceObject && source.catch) {
53+
catches.push(source.catch);
54+
}
4955
}
5056
});
51-
return queries;
57+
return {
58+
queries,
59+
catches
60+
};
5261
};
5362

54-
const testQueries = getTestQueries(sources);
63+
const testObjects = getTestObjects(sources);
5564

5665
super(`select:${sourceIds.join(":")}`, defaultValue, options);
5766

5867
this._sources = sources;
5968
this._resultsParser = args[lastIndex];
60-
this.test.queries = testQueries;
69+
this.test.queries = testObjects.queries;
70+
this.test.catches = testObjects.catches;
6171
this.test.selector = this._resultsParser;
6272
}
6373

test/Selector.testMethods.js

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ test.describe("Selector test methods", () => {
1515
sandbox = test.sinon.createSandbox();
1616
spies = {
1717
query: sandbox.spy(),
18-
selector: sandbox.spy()
18+
catch: sandbox.spy(),
19+
selector: sandbox.stub().callsFake(result => result)
1920
};
2021
TestOrigin = class extends Origin {
2122
_read() {
@@ -29,12 +30,13 @@ test.describe("Selector test methods", () => {
2930
query: query => {
3031
spies.query();
3132
return query;
33+
},
34+
catch: err => {
35+
spies.catch();
36+
return err;
3237
}
3338
},
34-
originResult => {
35-
spies.selector();
36-
return originResult;
37-
}
39+
spies.selector
3840
);
3941
});
4042

@@ -43,15 +45,15 @@ test.describe("Selector test methods", () => {
4345
sources.clear();
4446
});
4547

46-
test.describe("origin query functions", () => {
48+
test.describe("sources query functions", () => {
4749
test.describe("when there are no concurrent queries", () => {
4850
test.it("should be avaible for testing at the test.queries property", () => {
4951
test.expect(testSelector.test.queries[0]("foo")).to.equal("foo");
5052
test.expect(spies.query).to.have.been.called();
5153
});
5254
});
5355

54-
test.describe("when there are concurrent queries", () => {
56+
test.describe("when there are concurrent sources", () => {
5557
test.it("should be avaible for testing at the test.queries property as an array", () => {
5658
const testOrigin2 = new TestOrigin();
5759
testSelector = new Selector(
@@ -71,10 +73,7 @@ test.describe("Selector test methods", () => {
7173
}
7274
}
7375
],
74-
originResult => {
75-
spies.selector();
76-
return originResult;
77-
}
76+
spies.selector
7877
);
7978
test.expect(testSelector.test.queries[0][0]("foo")).to.equal("foo-1");
8079
test.expect(testSelector.test.queries[0][1]("foo")).to.equal("foo-2");
@@ -110,10 +109,7 @@ test.describe("Selector test methods", () => {
110109
}
111110
]
112111
],
113-
originResult => {
114-
spies.selector();
115-
return originResult;
116-
}
112+
spies.selector
117113
);
118114
test.expect(testSelector.test.queries[0][0]("foo")).to.equal("foo-1");
119115
test.expect(testSelector.test.queries[0][1][0]("foo")).to.equal("foo-2");
@@ -123,6 +119,80 @@ test.describe("Selector test methods", () => {
123119
});
124120
});
125121

122+
test.describe("sources catch functions", () => {
123+
test.describe("when there are no concurrent sources", () => {
124+
test.it("should be avaible for testing at the test.catches property", () => {
125+
test.expect(testSelector.test.catches[0]("foo")).to.equal("foo");
126+
test.expect(spies.catch).to.have.been.called();
127+
});
128+
});
129+
130+
test.describe("when there are concurrent sources", () => {
131+
test.it("should be avaible for testing at the test.catches property as an array", () => {
132+
const testOrigin2 = new TestOrigin();
133+
testSelector = new Selector(
134+
[
135+
{
136+
source: testOrigin,
137+
catch: err => {
138+
spies.catch();
139+
return `${err}-a`;
140+
}
141+
},
142+
{
143+
source: testOrigin2,
144+
catch: err => {
145+
spies.catch();
146+
return `${err}-b`;
147+
}
148+
}
149+
],
150+
spies.selector
151+
);
152+
test.expect(testSelector.test.catches[0][0]("foo")).to.equal("foo-a");
153+
test.expect(testSelector.test.catches[0][1]("foo")).to.equal("foo-b");
154+
test.expect(spies.catch).to.have.been.calledTwice();
155+
});
156+
157+
test.it("should have all concurrent catches available recursively", () => {
158+
const testOrigin2 = new TestOrigin();
159+
const testOrigin3 = new TestOrigin();
160+
testSelector = new Selector(
161+
[
162+
{
163+
source: testOrigin,
164+
catch: err => {
165+
spies.catch();
166+
return `${err}-a`;
167+
}
168+
},
169+
[
170+
{
171+
source: testOrigin2,
172+
catch: err => {
173+
spies.catch();
174+
return `${err}-b`;
175+
}
176+
},
177+
{
178+
source: testOrigin3,
179+
catch: err => {
180+
spies.catch();
181+
return `${err}-c`;
182+
}
183+
}
184+
]
185+
],
186+
spies.selector
187+
);
188+
test.expect(testSelector.test.catches[0][0]("foo")).to.equal("foo-a");
189+
test.expect(testSelector.test.catches[0][1][0]("foo")).to.equal("foo-b");
190+
test.expect(testSelector.test.catches[0][1][1]("foo")).to.equal("foo-c");
191+
test.expect(spies.catch.callCount).to.equal(3);
192+
});
193+
});
194+
});
195+
126196
test.describe("selector function", () => {
127197
test.it("should be avaible for testing at the test.selector property", () => {
128198
test.expect(testSelector.test.selector("foo")).to.equal("foo");

0 commit comments

Comments
 (0)