Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions modules/ui/src/app/mocks/profile.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,34 @@ export const RENAME_PROFILE_MOCK = {
export const COPY_PROFILE_MOCK: Profile = {
name: 'Copy of Primary profile',
status: ProfileStatus.VALID,
created: '2024-05-23 12:38:26',
questions: [
{
question: 'What is the email of the device owner(s)?',
answer: 'boddey@google.com, cmeredith@google.com',
},
{
question: 'What type of device do you need reviewed?',
answer: 'IoT Sensor',
},
{
question: 'Are any of the following statements true about your device?',
answer: 'First',
},
{
question: 'What features does the device have?',
answer: [0, 1, 2],
},
{
question: 'Comments',
answer: 'Yes',
},
],
};

export const DRAFT_COPY_PROFILE_MOCK: Profile = {
name: 'Copy of Primary profile',
status: ProfileStatus.DRAFT,
questions: [
{
question: 'What is the email of the device owner(s)?',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ProfileFormComponent } from './profile-form.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import {
COPY_PROFILE_MOCK,
DRAFT_COPY_PROFILE_MOCK,
NEW_PROFILE_MOCK,
NEW_PROFILE_MOCK_DRAFT,
OUTDATED_DRAFT_PROFILE_MOCK,
Expand Down Expand Up @@ -312,7 +313,7 @@ describe('ProfileFormComponent', () => {
});

it('should have an error when uses the name of copy profile', () => {
component.selectedProfile = COPY_PROFILE_MOCK;
component.selectedProfile = DRAFT_COPY_PROFILE_MOCK;
component.profiles = [PROFILE_MOCK, PROFILE_MOCK_2, COPY_PROFILE_MOCK];

expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import {
ValidatorFn,
} from '@angular/forms';
import { MatInputModule } from '@angular/material/input';
import { DeviceValidators } from '../../devices/components/device-form/device.validators';
import {
Profile,
ProfileFormat,
Expand Down Expand Up @@ -74,7 +73,6 @@ import { CdkTrapFocus } from '@angular/cdk/a11y';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ProfileFormComponent implements OnInit, AfterViewInit {
private deviceValidators = inject(DeviceValidators);
private profileValidators = inject(ProfileValidators);
private fb = inject(FormBuilder);

Expand All @@ -99,6 +97,9 @@ export class ProfileFormComponent implements OnInit, AfterViewInit {
}
@Input()
set selectedProfile(profile: Profile | null) {
if (this.isCopyProfile && this.profile) {
this.deleteCopy.emit(this.profile);
}
this.profile = profile;
if (profile && this.nameControl) {
this.updateNameValidator();
Expand All @@ -110,6 +111,7 @@ export class ProfileFormComponent implements OnInit, AfterViewInit {
}

@Output() saveProfile = new EventEmitter<Profile>();
@Output() deleteCopy = new EventEmitter<Profile>();
@Output() discard = new EventEmitter();
ngOnInit() {
this.profileForm = this.createProfileForm();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ export class ProfileValidators {
!profile.created ||
(profile.created && profile?.name.toLowerCase() !== value))
) {
const isSameProfileName = this.hasSameProfileName(value, profiles);
const isSameProfileName = this.hasSameProfileName(
value,
profiles,
profile?.created
);
return isSameProfileName ? { has_same_profile_name: true } : null;
}
return null;
Expand Down Expand Up @@ -98,11 +102,15 @@ export class ProfileValidators {

private hasSameProfileName(
profileName: string,
profiles: Profile[]
profiles: Profile[],
created?: string
): boolean {
return (
profiles.some(profile => profile.name.toLowerCase() === profileName) ||
false
profiles.some(
profile =>
profile.name.toLowerCase() === profileName &&
profile.created !== created
) || false
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ $profile-draft-icon-size: 22px;
$profile-icon-container-size: 24px;
$profile-item-container-gap: 8px;

:host.selected {
.profile-item-container {
background-color: colors.$grey-100;
}
}

.profile-item-container {
width: 100%;
height: 100%;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
[entities]="vm.profiles"
[isOpenEntityForm]="isOpenProfileForm"
[initialEntity]="vm.selectedProfile"
(addEntity)="openForm()">
(addEntity)="openForm()"
(menuItemClicked)="menuItemClicked($event, vm.profiles)">
</app-list-layout>

<ng-template #content>
Expand All @@ -42,6 +43,7 @@
[profiles]="vm.profiles"
[profileFormat]="vm.profileFormat"
(saveProfile)="saveProfileClicked($event, vm.selectedProfile)"
(deleteCopy)="deleteCopy($event, vm.profiles)"
(discard)="discard(vm.selectedProfile)"></app-profile-form>
</ng-template>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ import { TestRunService } from '../../services/test-run.service';
import SpyObj = jasmine.SpyObj;
import { MatSidenavModule } from '@angular/material/sidenav';
import {
COPY_PROFILE_MOCK,
DRAFT_COPY_PROFILE_MOCK,
NEW_PROFILE_MOCK,
NEW_PROFILE_MOCK_DRAFT,
PROFILE_MOCK,
} from '../../mocks/profile.mock';
import { of } from 'rxjs';
import { of, Subscription } from 'rxjs';
import { Component, Input } from '@angular/core';
import { Profile, ProfileAction, ProfileFormat } from '../../model/profile';
import { MatDialogRef } from '@angular/material/dialog';
import { SimpleDialogComponent } from '../../components/simple-dialog/simple-dialog.component';
import { RiskAssessmentStore } from './risk-assessment.store';
import { LiveAnnouncer } from '@angular/cdk/a11y';
import { Observable } from 'rxjs/internal/Observable';

describe('RiskAssessmentComponent', () => {
let component: RiskAssessmentComponent;
Expand All @@ -65,6 +66,7 @@ describe('RiskAssessmentComponent', () => {
'setFocusOnCreateButton',
'setFocusOnSelectedProfile',
'setFocusOnProfileForm',
'updateProfiles',
]);

await TestBed.configureTestingModule({
Expand Down Expand Up @@ -187,7 +189,7 @@ describe('RiskAssessmentComponent', () => {
} as MatDialogRef<typeof SimpleDialogComponent>);
tick();

component.deleteProfile(PROFILE_MOCK.name, 0, null);
component.deleteProfile(PROFILE_MOCK);
tick();

expect(openSpy).toHaveBeenCalledWith(SimpleDialogComponent, {
Expand All @@ -207,9 +209,22 @@ describe('RiskAssessmentComponent', () => {
spyOn(component.dialog, 'open').and.returnValue({
afterClosed: () => of(true),
} as MatDialogRef<typeof SimpleDialogComponent>);

mockRiskAssessmentStore.deleteProfile.and.callFake(
(
observableOrValue:
| { name: string; onDelete: (idx: number) => void }
| Observable<{ name: string; onDelete: (idx: number) => void }>
) => {
// @ts-expect-error onDelete exist in object
observableOrValue?.onDelete(1);
return new Subscription();
}
);

tick();

component.deleteProfile(PROFILE_MOCK.name, 0, PROFILE_MOCK);
component.deleteProfile(PROFILE_MOCK);
tick();

expect(
Expand Down Expand Up @@ -243,7 +258,7 @@ describe('RiskAssessmentComponent', () => {
describe('#getCopyOfProfile', () => {
it('should open the form with copy of profile', () => {
const copy = component.getCopyOfProfile(PROFILE_MOCK);
expect(copy).toEqual(COPY_PROFILE_MOCK);
expect(copy).toEqual(DRAFT_COPY_PROFILE_MOCK);
});
});

Expand All @@ -259,10 +274,13 @@ describe('RiskAssessmentComponent', () => {
it('#copyProfileAndOpenForm should call openForm with copy of profile', fakeAsync(() => {
spyOn(component, 'openForm');

component.copyProfileAndOpenForm(PROFILE_MOCK);
component.copyProfileAndOpenForm(PROFILE_MOCK, [
PROFILE_MOCK,
PROFILE_MOCK,
]);
tick();

expect(component.openForm).toHaveBeenCalledWith(COPY_PROFILE_MOCK);
expect(component.openForm).toHaveBeenCalledWith(DRAFT_COPY_PROFILE_MOCK);
}));

describe('#saveProfile', () => {
Expand Down
Loading