-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into chore/external-services-ux-improvements
- Loading branch information
Showing
35 changed files
with
1,387 additions
and
989 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
src/app/components/inspection/inspection-json.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<mat-toolbar> | ||
<section> | ||
<span i18n>Raw JSON</span> | ||
<button mat-icon-button (click)="copy()" i18n-matTooltip matTooltip="Copy JSON"> | ||
<mat-icon [fontIcon]="copyIcon" /> | ||
</button> | ||
</section> | ||
<button mat-icon-button (click)="toggleDisplay()" [matTooltip]="display ? hideToolTip : displayToolTip"> | ||
@if (!display) { | ||
<mat-icon [fontIcon]="arrowDownIcon" /> | ||
} @else { | ||
<mat-icon [fontIcon]="arrowUpIcon" /> | ||
} | ||
</button> | ||
</mat-toolbar> | ||
@if (display) { | ||
<mat-card> | ||
<span>{</span> | ||
<app-json [data]="data" /> | ||
<span>}</span> | ||
</mat-card> | ||
} |
87 changes: 87 additions & 0 deletions
87
src/app/components/inspection/inspection-json.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Clipboard } from '@angular/cdk/clipboard'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { IconsService } from '@services/icons.service'; | ||
import { NotificationService } from '@services/notification.service'; | ||
import { InspectionJsonComponent } from './inspection-json.component'; | ||
|
||
describe('InspectionJsonComponent', () => { | ||
let component: InspectionJsonComponent; | ||
|
||
const data = { | ||
one: 1, | ||
two: 2, | ||
object: { | ||
three: 3 | ||
}, | ||
array: [1, 2, 3] | ||
}; | ||
|
||
const mockNotificationService = { | ||
success: jest.fn(), | ||
error: jest.fn(), | ||
}; | ||
|
||
const mockClipboard = { | ||
copy: jest.fn(), | ||
}; | ||
|
||
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
||
beforeEach(() => { | ||
component = TestBed.configureTestingModule({ | ||
providers: [ | ||
InspectionJsonComponent, | ||
{ provide: NotificationService, useValue: mockNotificationService }, | ||
{ provide: Clipboard, useValue: mockClipboard }, | ||
IconsService, | ||
] | ||
}).inject(InspectionJsonComponent); | ||
component.data = data; | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should set data', () => { | ||
expect(component.data).toEqual(data); | ||
}); | ||
|
||
describe('copy', () => { | ||
it('should copy to clipboard', () => { | ||
component.copy(); | ||
expect(mockClipboard.copy).toHaveBeenCalledWith(JSON.stringify(data, null, 2)); | ||
}); | ||
|
||
it('should notify on copy', () => { | ||
component.copy(); | ||
expect(mockNotificationService.success).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should log in case of an error', () => { | ||
jest.spyOn(JSON, 'stringify').mockImplementationOnce(() => {throw new Error;}); | ||
component.copy(); | ||
expect(errorSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should notify in case of an error', () => { | ||
jest.spyOn(JSON, 'stringify').mockImplementationOnce(() => {throw new Error;}); | ||
component.copy(); | ||
expect(mockNotificationService.error).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('toggleDisplay', () => { | ||
it('should set display to true if it is false', () => { | ||
component.display = false; | ||
component.toggleDisplay(); | ||
expect(component.display).toBeTruthy(); | ||
}); | ||
|
||
it('should set display to false if it is true', () => { | ||
component.display = true; | ||
component.toggleDisplay(); | ||
expect(component.display).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
75 changes: 75 additions & 0 deletions
75
src/app/components/inspection/inspection-json.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Clipboard } from '@angular/cdk/clipboard'; | ||
import { Component, Input, inject } from '@angular/core'; | ||
import { MatButtonModule } from '@angular/material/button'; | ||
import { MatCardModule } from '@angular/material/card'; | ||
import { MatIconModule } from '@angular/material/icon'; | ||
import { MatToolbarModule } from '@angular/material/toolbar'; | ||
import { MatTooltipModule } from '@angular/material/tooltip'; | ||
import { IconsService } from '@services/icons.service'; | ||
import { NotificationService } from '@services/notification.service'; | ||
import { JsonComponent } from './json.component'; | ||
|
||
@Component({ | ||
selector: 'app-json-inspection', | ||
templateUrl: 'inspection-json.component.html', | ||
standalone: true, | ||
providers: [ | ||
IconsService, | ||
], | ||
imports: [ | ||
JsonComponent, | ||
MatIconModule, | ||
MatButtonModule, | ||
MatToolbarModule, | ||
MatCardModule, | ||
MatTooltipModule, | ||
], | ||
styles: [` | ||
mat-card { | ||
padding-top: 0.5rem; | ||
padding-bottom: 0.5rem; | ||
padding-left: 1rem; | ||
} | ||
mat-toolbar { | ||
display: flex; | ||
justify-content: space-between; | ||
} | ||
section { | ||
display: flex; | ||
align-items: center; | ||
} | ||
`] | ||
}) | ||
export class InspectionJsonComponent { | ||
@Input({ required: true }) data: object | null; | ||
|
||
display = false; | ||
|
||
readonly iconsService = inject(IconsService); | ||
readonly clipboard = inject(Clipboard); | ||
readonly notificationService = inject(NotificationService); | ||
|
||
readonly copyIcon = this.iconsService.getIcon('copy'); | ||
readonly arrowDownIcon = this.iconsService.getIcon('arrow-down'); | ||
readonly arrowUpIcon = this.iconsService.getIcon('arrow-up'); | ||
|
||
readonly displayToolTip = $localize`Display JSON`; | ||
readonly hideToolTip = $localize`Hide JSON`; | ||
|
||
copy() { | ||
try { | ||
const object = JSON.stringify(this.data, null, 2); | ||
this.clipboard.copy(object); | ||
this.notificationService.success('JSON copied to clipboard'); | ||
} catch (e) { | ||
console.error(e); | ||
this.notificationService.error('Could not copy JSON'); | ||
} | ||
} | ||
|
||
toggleDisplay() { | ||
this.display = !this.display; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
@for(key of keys; track key) { | ||
<section> | ||
@if(isObject(key) && !isArray(key)) { | ||
<p>{{ key }}: {</p> | ||
<app-json [data]="data[key]" /> | ||
<p>}</p> | ||
} @else if (data[key]) { | ||
<p>{{key}}: {{ data[key] | json }}</p> | ||
} @else { | ||
<p>{{key}}: undefined</p> | ||
} | ||
</section> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { JsonComponent } from './json.component'; | ||
|
||
describe('JsonComponent', () => { | ||
const component = new JsonComponent(); | ||
|
||
const data = { | ||
one: 1, | ||
two: 2, | ||
object: { | ||
three: 3 | ||
}, | ||
array: [1, 2, 3] | ||
}; | ||
|
||
beforeEach(() => { | ||
component.data = data; | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should set data', () => { | ||
expect(component.data).toEqual(data); | ||
}); | ||
|
||
it('should set keys', () => { | ||
expect(component.keys).toEqual(['one', 'two', 'object', 'array']); | ||
}); | ||
|
||
describe('isArray', () => { | ||
it('should return true if an array is passed', () => { | ||
expect(component.isArray('array' as keyof object)).toBeTruthy(); | ||
}); | ||
|
||
it('should return false if an object is passed', () => { | ||
expect(component.isArray('object' as keyof object)).toBeFalsy(); | ||
}); | ||
|
||
it('should return false if a simple value is passed', () => { | ||
expect(component.isArray('one' as keyof object)).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('isObject', () => { | ||
it('should return true if an object is passed', () => { | ||
expect(component.isObject('object' as keyof object)).toBeTruthy(); | ||
}); | ||
|
||
it('should return true if an array is passed', () => { | ||
expect(component.isObject('array' as keyof object)).toBeTruthy(); | ||
}); | ||
|
||
it('should return false if a simple value is passed', () => { | ||
expect(component.isObject('one' as keyof object)).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { JsonPipe } from '@angular/common'; | ||
import { Component, Input } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-json', | ||
templateUrl: 'json.component.html', | ||
standalone: true, | ||
imports: [ | ||
JsonPipe | ||
], | ||
styles: [` | ||
section { | ||
margin-left: 1rem; | ||
} | ||
p { | ||
margin: 0; | ||
} | ||
`] | ||
}) | ||
export class JsonComponent { | ||
private _data: object; | ||
|
||
keys: (keyof object)[]; | ||
|
||
@Input({ required: true }) set data(entry: object | null) { | ||
if(entry) { | ||
this.keys = Object.keys(entry).map(key => key.replace('_', '')) as keyof object; | ||
this._data = entry; | ||
} | ||
} | ||
|
||
get data(): object { | ||
return this._data; | ||
} | ||
|
||
isArray(key: keyof object): boolean { | ||
return this.data[key] && (this.data[key] as Array<string>).length; | ||
} | ||
|
||
isObject(key: keyof object): boolean { | ||
return (this._data[key] as unknown) instanceof Object; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.