Skip to content
This repository was archived by the owner on Mar 25, 2023. It is now read-only.

Commit a328f76

Browse files
committed
feat(security-group): select first not preselected sg (#595)
1 parent f557afc commit a328f76

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed
Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,66 @@
11
import { TestBed } from '@angular/core/testing';
2+
import { SimpleChange } from '@angular/core';
3+
24
import { VmsSgListComponent } from './vms-sg-list.component';
3-
import { SecurityGroup } from '../../../security-group/sg.model';
5+
import { getMockSecurityGroup } from '../../../../testutils/mocks/mocks';
46

57
describe('VmsSgListComponent', () => {
68
let comp: VmsSgListComponent;
7-
const securityGroup = { id: 'sg-1', name: 'name', isPreselected: false } as SecurityGroup;
89

910
beforeEach(() => {
1011
TestBed.configureTestingModule({});
1112

1213
comp = new VmsSgListComponent();
1314
});
1415

15-
it('should select security group', () => {
16-
comp.selectSecurityGroup(securityGroup);
17-
expect(comp.currentSelectedSecurityGroup).toBe(securityGroup);
16+
describe('selectSecurityGroup', () => {
17+
it('should select security group', () => {
18+
const securityGroup = getMockSecurityGroup({ id: 'sg-1', isPreselected: false });
19+
comp.selectSecurityGroup(securityGroup);
20+
expect(comp.currentSelectedSecurityGroup).toBe(securityGroup);
21+
});
22+
23+
it('should not select preselected security group', () => {
24+
const securityGroup = getMockSecurityGroup({ id: 'sg-1', isPreselected: true });
25+
comp.selectSecurityGroup(securityGroup);
26+
expect(comp.currentSelectedSecurityGroup).toBeUndefined();
27+
});
28+
});
29+
30+
describe('ngOnChanges', () => {
31+
it('should change currentSelectedSecurityGroup', () => {
32+
const securityGroups = [
33+
getMockSecurityGroup({ id: 'first_preselected', isPreselected: true }),
34+
getMockSecurityGroup({ id: 'first_not_selected', isPreselected: false }),
35+
getMockSecurityGroup({ id: 'second_not_selected', isPreselected: false }),
36+
];
37+
comp.securityGroups = securityGroups;
38+
comp.ngOnChanges({ securityGroups: new SimpleChange(null, securityGroups, false) });
39+
expect(comp.currentSelectedSecurityGroup).toEqual(securityGroups[1]);
40+
});
41+
42+
it('should not change currentSelectedSecurityGroup if it was selected', () => {
43+
const securityGroups = [
44+
getMockSecurityGroup({ id: 'first_preselected', isPreselected: true }),
45+
getMockSecurityGroup({ id: 'first_not_selected', isPreselected: false }),
46+
getMockSecurityGroup({ id: 'second_not_selected', isPreselected: false }),
47+
];
48+
comp.currentSelectedSecurityGroup = securityGroups[2];
49+
comp.securityGroups = securityGroups;
50+
comp.ngOnChanges({ securityGroups: new SimpleChange([], securityGroups, false) });
51+
expect(comp.currentSelectedSecurityGroup).toEqual(securityGroups[2]);
52+
});
53+
54+
it('should change currentSelectedSecurityGroup if it not in securityGroups', () => {
55+
const securityGroups = [
56+
getMockSecurityGroup({ id: 'first_preselected', isPreselected: true }),
57+
getMockSecurityGroup({ id: 'first_not_selected', isPreselected: false }),
58+
getMockSecurityGroup({ id: 'second_not_selected', isPreselected: false }),
59+
];
60+
comp.currentSelectedSecurityGroup = getMockSecurityGroup();
61+
comp.securityGroups = securityGroups;
62+
comp.ngOnChanges({ securityGroups: new SimpleChange([], securityGroups, false) });
63+
expect(comp.currentSelectedSecurityGroup).toEqual(securityGroups[1]);
64+
});
1865
});
1966
});

src/app/vm/vm-sidebar/vms-sg-list/vms-sg-list.component.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Component, EventEmitter, Input, Output } from '@angular/core';
1+
import { Component, EventEmitter, Input, OnChanges, Output, SimpleChanges } from '@angular/core';
22
import { SecurityGroup } from '../../../security-group/sg.model';
33

44
@Component({
55
selector: 'cs-vms-sg-list',
66
templateUrl: 'vms-sg-list.component.html',
77
styleUrls: ['vms-sg-list.component.scss'],
88
})
9-
export class VmsSgListComponent {
9+
export class VmsSgListComponent implements OnChanges {
1010
@Input()
1111
public securityGroups: SecurityGroup[];
1212
@Input()
@@ -23,4 +23,15 @@ export class VmsSgListComponent {
2323
this.currentSelectedSecurityGroup = sg;
2424
}
2525
}
26+
27+
public ngOnChanges(changes: SimpleChanges): void {
28+
// select the first not preselected sg
29+
if (
30+
changes['securityGroups'].currentValue &&
31+
(!this.currentSelectedSecurityGroup ||
32+
!this.securityGroups.includes(this.currentSelectedSecurityGroup))
33+
) {
34+
this.currentSelectedSecurityGroup = this.securityGroups.find(sg => !sg.isPreselected);
35+
}
36+
}
2637
}

src/testutils/mocks/mocks.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { SecurityGroup } from '../../app/security-group/sg.model';
2+
3+
export function getMockSecurityGroup(updates: Partial<SecurityGroup> = {}): SecurityGroup {
4+
return {
5+
id: null,
6+
account: null,
7+
description: null,
8+
domain: null,
9+
domainid: null,
10+
name: null,
11+
tags: [],
12+
virtualmachinecount: null,
13+
virtualmachineids: [],
14+
egressrule: [],
15+
ingressrule: [],
16+
...updates,
17+
};
18+
}

0 commit comments

Comments
 (0)