1 | import { __awaiter } from 'tslib';
|
---|
2 | import { ContentContainerComponentHarness, HarnessPredicate, ComponentHarness, parallel } from '@angular/cdk/testing';
|
---|
3 | import { coerceBooleanProperty, coerceNumberProperty } from '@angular/cdk/coercion';
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * @license
|
---|
7 | * Copyright Google LLC All Rights Reserved.
|
---|
8 | *
|
---|
9 | * Use of this source code is governed by an MIT-style license that can be
|
---|
10 | * found in the LICENSE file at https://angular.io/license
|
---|
11 | */
|
---|
12 | /** Harness for interacting with a standard Angular Material tree node. */
|
---|
13 | class MatTreeNodeHarness extends ContentContainerComponentHarness {
|
---|
14 | constructor() {
|
---|
15 | super(...arguments);
|
---|
16 | this._toggle = this.locatorForOptional('[matTreeNodeToggle]');
|
---|
17 | }
|
---|
18 | /**
|
---|
19 | * Gets a `HarnessPredicate` that can be used to search for a tree node with specific attributes.
|
---|
20 | * @param options Options for narrowing the search
|
---|
21 | * @return a `HarnessPredicate` configured with the given options.
|
---|
22 | */
|
---|
23 | static with(options = {}) {
|
---|
24 | return getNodePredicate(MatTreeNodeHarness, options);
|
---|
25 | }
|
---|
26 | /** Whether the tree node is expanded. */
|
---|
27 | isExpanded() {
|
---|
28 | return __awaiter(this, void 0, void 0, function* () {
|
---|
29 | return coerceBooleanProperty(yield (yield this.host()).getAttribute('aria-expanded'));
|
---|
30 | });
|
---|
31 | }
|
---|
32 | /** Whether the tree node is disabled. */
|
---|
33 | isDisabled() {
|
---|
34 | return __awaiter(this, void 0, void 0, function* () {
|
---|
35 | return coerceBooleanProperty(yield (yield this.host()).getProperty('aria-disabled'));
|
---|
36 | });
|
---|
37 | }
|
---|
38 | /** Gets the level of the tree node. Note that this gets the aria-level and is 1 indexed. */
|
---|
39 | getLevel() {
|
---|
40 | return __awaiter(this, void 0, void 0, function* () {
|
---|
41 | return coerceNumberProperty(yield (yield this.host()).getAttribute('aria-level'));
|
---|
42 | });
|
---|
43 | }
|
---|
44 | /** Gets the tree node's text. */
|
---|
45 | getText() {
|
---|
46 | return __awaiter(this, void 0, void 0, function* () {
|
---|
47 | return (yield this.host()).text({ exclude: '.mat-tree-node, .mat-nested-tree-node, button' });
|
---|
48 | });
|
---|
49 | }
|
---|
50 | /** Toggles node between expanded/collapsed. Only works when node is not disabled. */
|
---|
51 | toggle() {
|
---|
52 | return __awaiter(this, void 0, void 0, function* () {
|
---|
53 | const toggle = yield this._toggle();
|
---|
54 | if (toggle) {
|
---|
55 | return toggle.click();
|
---|
56 | }
|
---|
57 | });
|
---|
58 | }
|
---|
59 | /** Expands the node if it is collapsed. Only works when node is not disabled. */
|
---|
60 | expand() {
|
---|
61 | return __awaiter(this, void 0, void 0, function* () {
|
---|
62 | if (!(yield this.isExpanded())) {
|
---|
63 | yield this.toggle();
|
---|
64 | }
|
---|
65 | });
|
---|
66 | }
|
---|
67 | /** Collapses the node if it is expanded. Only works when node is not disabled. */
|
---|
68 | collapse() {
|
---|
69 | return __awaiter(this, void 0, void 0, function* () {
|
---|
70 | if (yield this.isExpanded()) {
|
---|
71 | yield this.toggle();
|
---|
72 | }
|
---|
73 | });
|
---|
74 | }
|
---|
75 | }
|
---|
76 | /** The selector of the host element of a `MatTreeNode` instance. */
|
---|
77 | MatTreeNodeHarness.hostSelector = '.mat-tree-node, .mat-nested-tree-node';
|
---|
78 | function getNodePredicate(type, options) {
|
---|
79 | return new HarnessPredicate(type, options)
|
---|
80 | .addOption('text', options.text, (harness, text) => HarnessPredicate.stringMatches(harness.getText(), text))
|
---|
81 | .addOption('disabled', options.disabled, (harness, disabled) => __awaiter(this, void 0, void 0, function* () { return (yield harness.isDisabled()) === disabled; }))
|
---|
82 | .addOption('expanded', options.expanded, (harness, expanded) => __awaiter(this, void 0, void 0, function* () { return (yield harness.isExpanded()) === expanded; }))
|
---|
83 | .addOption('level', options.level, (harness, level) => __awaiter(this, void 0, void 0, function* () { return (yield harness.getLevel()) === level; }));
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * @license
|
---|
88 | * Copyright Google LLC All Rights Reserved.
|
---|
89 | *
|
---|
90 | * Use of this source code is governed by an MIT-style license that can be
|
---|
91 | * found in the LICENSE file at https://angular.io/license
|
---|
92 | */
|
---|
93 | /** Harness for interacting with a standard mat-tree in tests. */
|
---|
94 | class MatTreeHarness extends ComponentHarness {
|
---|
95 | /**
|
---|
96 | * Gets a `HarnessPredicate` that can be used to search for a tree with specific attributes.
|
---|
97 | * @param options Options for narrowing the search
|
---|
98 | * @return a `HarnessPredicate` configured with the given options.
|
---|
99 | */
|
---|
100 | static with(options = {}) {
|
---|
101 | return new HarnessPredicate(MatTreeHarness, options);
|
---|
102 | }
|
---|
103 | /** Gets all of the nodes in the tree. */
|
---|
104 | getNodes(filter = {}) {
|
---|
105 | return __awaiter(this, void 0, void 0, function* () {
|
---|
106 | return this.locatorForAll(MatTreeNodeHarness.with(filter))();
|
---|
107 | });
|
---|
108 | }
|
---|
109 | /**
|
---|
110 | * Gets an object representation for the visible tree structure
|
---|
111 | * If a node is under an unexpanded node it will not be included.
|
---|
112 | * Eg.
|
---|
113 | * Tree (all nodes expanded):
|
---|
114 | * `
|
---|
115 | * <mat-tree>
|
---|
116 | * <mat-tree-node>Node 1<mat-tree-node>
|
---|
117 | * <mat-nested-tree-node>
|
---|
118 | * Node 2
|
---|
119 | * <mat-nested-tree-node>
|
---|
120 | * Node 2.1
|
---|
121 | * <mat-tree-node>
|
---|
122 | * Node 2.1.1
|
---|
123 | * <mat-tree-node>
|
---|
124 | * <mat-nested-tree-node>
|
---|
125 | * <mat-tree-node>
|
---|
126 | * Node 2.2
|
---|
127 | * <mat-tree-node>
|
---|
128 | * <mat-nested-tree-node>
|
---|
129 | * </mat-tree>`
|
---|
130 | *
|
---|
131 | * Tree structure:
|
---|
132 | * {
|
---|
133 | * children: [
|
---|
134 | * {
|
---|
135 | * text: 'Node 1',
|
---|
136 | * children: [
|
---|
137 | * {
|
---|
138 | * text: 'Node 2',
|
---|
139 | * children: [
|
---|
140 | * {
|
---|
141 | * text: 'Node 2.1',
|
---|
142 | * children: [{text: 'Node 2.1.1'}]
|
---|
143 | * },
|
---|
144 | * {text: 'Node 2.2'}
|
---|
145 | * ]
|
---|
146 | * }
|
---|
147 | * ]
|
---|
148 | * }
|
---|
149 | * ]
|
---|
150 | * };
|
---|
151 | */
|
---|
152 | getTreeStructure() {
|
---|
153 | return __awaiter(this, void 0, void 0, function* () {
|
---|
154 | const nodes = yield this.getNodes();
|
---|
155 | const nodeInformation = yield parallel(() => nodes.map(node => {
|
---|
156 | return parallel(() => [node.getLevel(), node.getText(), node.isExpanded()]);
|
---|
157 | }));
|
---|
158 | return this._getTreeStructure(nodeInformation, 1, true);
|
---|
159 | });
|
---|
160 | }
|
---|
161 | /**
|
---|
162 | * Recursively collect the structured text of the tree nodes.
|
---|
163 | * @param nodes A list of tree nodes
|
---|
164 | * @param level The level of nodes that are being accounted for during this iteration
|
---|
165 | * @param parentExpanded Whether the parent of the first node in param nodes is expanded
|
---|
166 | */
|
---|
167 | _getTreeStructure(nodes, level, parentExpanded) {
|
---|
168 | var _a, _b, _c;
|
---|
169 | const result = {};
|
---|
170 | for (let i = 0; i < nodes.length; i++) {
|
---|
171 | const [nodeLevel, text, expanded] = nodes[i];
|
---|
172 | const nextNodeLevel = (_b = (_a = nodes[i + 1]) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : -1;
|
---|
173 | // Return the accumulated value for the current level once we reach a shallower level node
|
---|
174 | if (nodeLevel < level) {
|
---|
175 | return result;
|
---|
176 | }
|
---|
177 | // Skip deeper level nodes during this iteration, they will be picked up in a later iteration
|
---|
178 | if (nodeLevel > level) {
|
---|
179 | continue;
|
---|
180 | }
|
---|
181 | // Only add to representation if it is visible (parent is expanded)
|
---|
182 | if (parentExpanded) {
|
---|
183 | // Collect the data under this node according to the following rules:
|
---|
184 | // 1. If the next node in the list is a sibling of the current node add it to the child list
|
---|
185 | // 2. If the next node is a child of the current node, get the sub-tree structure for the
|
---|
186 | // child and add it under this node
|
---|
187 | // 3. If the next node has a shallower level, we've reached the end of the child nodes for
|
---|
188 | // the current parent.
|
---|
189 | if (nextNodeLevel === level) {
|
---|
190 | this._addChildToNode(result, { text });
|
---|
191 | }
|
---|
192 | else if (nextNodeLevel > level) {
|
---|
193 | let children = (_c = this._getTreeStructure(nodes.slice(i + 1), nextNodeLevel, expanded)) === null || _c === void 0 ? void 0 : _c.children;
|
---|
194 | let child = children ? { text, children } : { text };
|
---|
195 | this._addChildToNode(result, child);
|
---|
196 | }
|
---|
197 | else {
|
---|
198 | this._addChildToNode(result, { text });
|
---|
199 | return result;
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 | return result;
|
---|
204 | }
|
---|
205 | _addChildToNode(result, child) {
|
---|
206 | result.children ? result.children.push(child) : result.children = [child];
|
---|
207 | }
|
---|
208 | }
|
---|
209 | /** The selector for the host element of a `MatTableHarness` instance. */
|
---|
210 | MatTreeHarness.hostSelector = '.mat-tree';
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * @license
|
---|
214 | * Copyright Google LLC All Rights Reserved.
|
---|
215 | *
|
---|
216 | * Use of this source code is governed by an MIT-style license that can be
|
---|
217 | * found in the LICENSE file at https://angular.io/license
|
---|
218 | */
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * @license
|
---|
222 | * Copyright Google LLC All Rights Reserved.
|
---|
223 | *
|
---|
224 | * Use of this source code is governed by an MIT-style license that can be
|
---|
225 | * found in the LICENSE file at https://angular.io/license
|
---|
226 | */
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * @license
|
---|
230 | * Copyright Google LLC All Rights Reserved.
|
---|
231 | *
|
---|
232 | * Use of this source code is governed by an MIT-style license that can be
|
---|
233 | * found in the LICENSE file at https://angular.io/license
|
---|
234 | */
|
---|
235 |
|
---|
236 | export { MatTreeHarness, MatTreeNodeHarness };
|
---|
237 | //# sourceMappingURL=testing.js.map
|
---|