Skip to content

Commit 0a72bfe

Browse files
fix: error count determination in Angular Material ArrayLayout
1 parent f815e1c commit 0a72bfe

File tree

2 files changed

+179
-1
lines changed

2 files changed

+179
-1
lines changed

packages/angular-material/src/library/layouts/array-layout.renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import {
6060
<mat-icon
6161
*ngIf="this.error?.length"
6262
color="warn"
63-
matBadge="{{ this.error.split('').length }}"
63+
matBadge="{{ this.error.split('\\n').length }}"
6464
matBadgeColor="warn"
6565
matTooltip="{{ this.error }}"
6666
matTooltipClass="error-message-tooltip"
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2017-2019 EclipseSource Munich
5+
https://github.com/eclipsesource/jsonforms
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
import { ComponentFixture, waitForAsync } from '@angular/core/testing';
26+
import { MatIcon } from '@angular/material/icon';
27+
import { MatBadge } from '@angular/material/badge';
28+
import { MatTooltip } from '@angular/material/tooltip';
29+
import {
30+
MatCard,
31+
MatCardContent,
32+
MatCardActions,
33+
} from '@angular/material/card';
34+
import { beforeEachLayoutTest, setupMockStore } from './common';
35+
import {
36+
ArrayLayoutRenderer,
37+
ArrayLayoutRendererTester,
38+
} from '../src/library/layouts/array-layout.renderer';
39+
import { LayoutChildrenRenderPropsPipe } from '../src/library/layouts/layout.renderer';
40+
41+
const TEST_SCHEMA = {
42+
type: 'object',
43+
properties: {
44+
test: {
45+
type: 'array',
46+
minItems: 1,
47+
items: {
48+
type: 'object',
49+
properties: {
50+
test1: {
51+
type: 'string',
52+
title: 'Test 1',
53+
},
54+
test2: {
55+
type: 'string',
56+
title: 'Test 2',
57+
},
58+
},
59+
required: ['test1', 'test2'],
60+
},
61+
},
62+
},
63+
required: ['test'],
64+
};
65+
66+
const TEST_UISCHEMA = {
67+
type: 'Control',
68+
scope: '#/properties/test',
69+
options: {
70+
detail: {
71+
type: 'HorizontalLayout',
72+
elements: [
73+
{
74+
type: 'Control',
75+
scope: '#/properties/test1',
76+
},
77+
{
78+
type: 'Control',
79+
scope: '#/properties/test2',
80+
},
81+
],
82+
},
83+
},
84+
};
85+
86+
describe('Array layout tester', () => {
87+
it('should succeed', () => {
88+
expect(
89+
ArrayLayoutRendererTester(TEST_UISCHEMA, TEST_SCHEMA, {
90+
config: {},
91+
rootSchema: {},
92+
})
93+
).toBe(4);
94+
});
95+
});
96+
describe('Array layout', () => {
97+
let fixture: ComponentFixture<any>;
98+
99+
beforeEach(waitForAsync(() => {
100+
fixture = beforeEachLayoutTest(ArrayLayoutRenderer, {
101+
declarations: [LayoutChildrenRenderPropsPipe],
102+
imports: [
103+
MatIcon,
104+
MatBadge,
105+
MatTooltip,
106+
MatCard,
107+
MatCardContent,
108+
MatCardActions,
109+
],
110+
});
111+
}));
112+
113+
it('render with no data the error count should be 1', () => {
114+
setupMockStore(fixture, {
115+
data: {},
116+
schema: TEST_SCHEMA,
117+
uischema: TEST_UISCHEMA,
118+
});
119+
fixture.componentInstance.ngOnInit();
120+
fixture.detectChanges();
121+
expect(fixture.nativeElement.children[0].children.length).toBe(2);
122+
123+
fixture.whenRenderingDone().then(() => {
124+
fixture.detectChanges();
125+
126+
const arrayLayoutElement: HTMLElement = fixture.nativeElement;
127+
const matBadgeElement =
128+
arrayLayoutElement.querySelector('.mat-badge-content')!;
129+
130+
const noDataElement = arrayLayoutElement.children[0].children[1];
131+
132+
expect(matBadgeElement.textContent).toBe('1');
133+
expect(noDataElement.textContent).toBe('No data');
134+
});
135+
});
136+
137+
it('render with data that contains empty required fields should show proper error count', () => {
138+
setupMockStore(fixture, {
139+
data: { test: [{}] },
140+
schema: TEST_SCHEMA,
141+
uischema: TEST_UISCHEMA,
142+
});
143+
fixture.componentInstance.ngOnInit();
144+
fixture.detectChanges();
145+
expect(fixture.nativeElement.children[0].children.length).toBe(2);
146+
147+
fixture.whenRenderingDone().then(() => {
148+
fixture.detectChanges();
149+
150+
const arrayLayoutElement: HTMLElement = fixture.nativeElement;
151+
const matBadgeElement =
152+
arrayLayoutElement.querySelector('.mat-badge-content')!;
153+
154+
expect(matBadgeElement.textContent).toBe('2');
155+
});
156+
});
157+
158+
it('render with more data that contains empty required fields should show proper error count', () => {
159+
setupMockStore(fixture, {
160+
data: { test: [{}, {}] },
161+
schema: TEST_SCHEMA,
162+
uischema: TEST_UISCHEMA,
163+
});
164+
fixture.componentInstance.ngOnInit();
165+
fixture.detectChanges();
166+
expect(fixture.nativeElement.children[0].children.length).toBe(3);
167+
168+
fixture.whenRenderingDone().then(() => {
169+
fixture.detectChanges();
170+
171+
const arrayLayoutElement: HTMLElement = fixture.nativeElement;
172+
const matBadgeElement =
173+
arrayLayoutElement.querySelector('.mat-badge-content')!;
174+
175+
expect(matBadgeElement.textContent).toBe('4');
176+
});
177+
});
178+
});

0 commit comments

Comments
 (0)