1 |
|
2 |
|
3 | function assertElement(element) {
|
4 | if (!(element instanceof HTMLElement)) {
|
5 | throw new Error('Must pass a DOM element to find/findAll(element, styledComponent)"');
|
6 | }
|
7 | }
|
8 |
|
9 | function assertStyledComponent(styledComponent) {
|
10 | if (
|
11 | !(styledComponent.styledComponentId && typeof styledComponent.styledComponentId === 'string')
|
12 | ) {
|
13 | throw new Error(
|
14 | `${styledComponent.displayName ||
|
15 | styledComponent.name ||
|
16 | 'Component'} is not a styled component.`
|
17 | );
|
18 | }
|
19 | }
|
20 |
|
21 | function enzymeFind(wrapper /* : Object */, styledComponent /* : Object */) {
|
22 | assertStyledComponent(styledComponent);
|
23 |
|
24 | return wrapper.find(`.${styledComponent.styledComponentId}`);
|
25 | }
|
26 |
|
27 | function find(element /* : Element */, styledComponent /* : Object */) {
|
28 | assertElement(element);
|
29 | assertStyledComponent(styledComponent);
|
30 | return element.querySelector(`.${styledComponent.styledComponentId}`);
|
31 | }
|
32 |
|
33 | function findAll(element /* : Element */, styledComponent /* : Object */) {
|
34 | assertElement(element);
|
35 | assertStyledComponent(styledComponent);
|
36 | return element.querySelectorAll(`.${styledComponent.styledComponentId}`);
|
37 | }
|
38 |
|
39 | exports.enzymeFind = enzymeFind;
|
40 | exports.find = find;
|
41 | exports.findAll = findAll;
|