Skip to content

Commit

Permalink
test: update for statuses components
Browse files Browse the repository at this point in the history
  • Loading branch information
fdewas-aneo committed Dec 24, 2024
1 parent 1809a97 commit 7f5f153
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,28 @@ describe('', () => {
close: jest.fn()
};

const statusesLabelsColors = {
[TaskStatus.TASK_STATUS_CANCELLED]: {
label: 'Cancelled',
color: 'black',
},
[TaskStatus.TASK_STATUS_COMPLETED]: {
label: 'Completed',
color: 'green',
},
[TaskStatus.TASK_STATUS_ERROR]: {
label: 'Error',
color: 'red',
}
};

beforeEach(() => {
component = TestBed.configureTestingModule({
providers: [
AddStatusesGroupDialogComponent,
{ provide: MatDialogRef, useValue: mockMatDialogRef },
{ provide: MAT_DIALOG_DATA, useValue: {
statuses: [{ name: 'result', value: 'the-result' }]
statuses: statusesLabelsColors,
} }
]
}).inject(AddStatusesGroupDialogComponent);
Expand All @@ -28,8 +43,7 @@ describe('', () => {
});

it('should init', () => {
component.ngOnInit();
expect(component.statuses).toEqual([{ name: 'result', value: 'the-result' }]);
expect(component.data.statuses).toEqual(statusesLabelsColors);
});

it('should close with result on submit', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { TaskStatus } from '@aneoconsultingfr/armonik.api.angular';
import { TestBed } from '@angular/core/testing';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { EditStatusesGroupDialogComponent } from './edit-status-group-dialog.component';
import { TasksStatusesGroup } from '../../dashboard/types';

describe('', () => {
let component: EditStatusesGroupDialogComponent;
Expand All @@ -11,14 +10,31 @@ describe('', () => {
close: jest.fn()
};

const statusesLabelsColors = {
[TaskStatus.TASK_STATUS_CANCELLED]: {
label: 'Cancelled',
color: 'black',
},
[TaskStatus.TASK_STATUS_COMPLETED]: {
label: 'Completed',
color: 'green',
},
[TaskStatus.TASK_STATUS_ERROR]: {
label: 'Error',
color: 'red',
}
};

const group = { name: 'status', color: 'green', statuses: [TaskStatus.TASK_STATUS_CANCELLED, TaskStatus.TASK_STATUS_COMPLETED]};

beforeEach(() => {
component = TestBed.configureTestingModule({
providers: [
EditStatusesGroupDialogComponent,
{ provide: MatDialogRef, useValue: mockMatDialogRef },
{ provide: MAT_DIALOG_DATA, useValue: {
statuses: [{ name: 'result', value: 'the-result' }],
group: { name: 'status', color: 'green', statuses: [TaskStatus.TASK_STATUS_CANCELLED, TaskStatus.TASK_STATUS_COMPLETED]}
statuses: statusesLabelsColors,
group: group,
} }
]
}).inject(EditStatusesGroupDialogComponent);
Expand All @@ -29,13 +45,11 @@ describe('', () => {
});

it('should init', () => {
component.ngOnInit();
expect(component.statuses).toEqual([{ name: 'result', value: 'the-result' }]);
expect(component.group).toEqual({ name: 'status', color: 'green', statuses: [TaskStatus.TASK_STATUS_CANCELLED, TaskStatus.TASK_STATUS_COMPLETED]});
expect(component.data.statuses).toEqual(statusesLabelsColors);
expect(component.group).toEqual(group);
});

it('should close with result on submit', () => {
const group: TasksStatusesGroup = {name: 'status', color: 'green', statuses: [TaskStatus.TASK_STATUS_CANCELLED, TaskStatus.TASK_STATUS_COMPLETED]};
component.onSubmit(group);
expect(mockMatDialogRef.close).toHaveBeenCalledWith(group);
});
Expand Down
127 changes: 74 additions & 53 deletions src/app/components/statuses/form-statuses-group.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import { TaskStatus } from '@aneoconsultingfr/armonik.api.angular';
import { MatCheckboxChange } from '@angular/material/checkbox';
import { StatusLabelColor } from '@app/types/status';
import { FormStatusesGroupComponent } from './form-statuses-group.component';

describe('FormStatusesGroupComponent', () => {
const component = new FormStatusesGroupComponent();

const statuses: { name: string, value: string }[] = [
{ name: 'Completed', value: `${TaskStatus.TASK_STATUS_COMPLETED}` },
{ name: 'Cancelled', value: `${TaskStatus.TASK_STATUS_CANCELLED}` },
{ name: 'Processed', value: `${TaskStatus.TASK_STATUS_PROCESSED}` }
];
const statusesLabelsColors = {
[TaskStatus.TASK_STATUS_CANCELLED]: {
label: 'Cancelled',
color: 'black',
},
[TaskStatus.TASK_STATUS_COMPLETED]: {
label: 'Completed',
color: 'green',
},
[TaskStatus.TASK_STATUS_PROCESSED]: {
label: 'Processed',
color: 'orange',
}
} as Record<TaskStatus, StatusLabelColor>;

const defaultGroup = {
name: 'status',
color: 'green',
statuses: [
TaskStatus.TASK_STATUS_CANCELLED,
TaskStatus.TASK_STATUS_COMPLETED
]
};

beforeEach(() => {
component.statuses = statuses;
component.group = {
name: 'status',
statuses: [
TaskStatus.TASK_STATUS_CANCELLED,
TaskStatus.TASK_STATUS_COMPLETED
]
};
component.statuses = statusesLabelsColors;
component.group = defaultGroup;
component.ngOnInit();
});

Expand All @@ -29,25 +42,33 @@ describe('FormStatusesGroupComponent', () => {

describe('on init', () => {
it('should complete form', () => {
expect(component.groupForm.value).toEqual(defaultGroup);
});

it('should init even with a empty group', () => {
component.group = null;
component.ngOnInit();
expect(component.groupForm.value).toEqual({
name: 'status',
name: null,
color: null,
statuses: [8, 4]
statuses: [],
});
});
});

it('should return true if it is checked', () => {
expect(component.isChecked({name: 'status', value: '4'})).toBeTruthy();
});

it('should return false if it is not checked', () => {
expect(component.isChecked({name: 'status', value: '5'})).toBeFalsy();
});

it('should return false by default', () => {
component.group = null;
expect(component.isChecked({name: 'status', value: '4'})).toBeFalsy();
describe('isChecked', () => {
it('should return true', () => {
expect(component.isChecked('4')).toBeTruthy();
});

it('should return false', () => {
expect(component.isChecked('5')).toBeFalsy();
});

it('should return false by default', () => {
component.group = null;
expect(component.isChecked('4')).toBeFalsy();
});
});

describe('onCheckboxChange', () => {
Expand Down Expand Up @@ -88,7 +109,7 @@ describe('FormStatusesGroupComponent', () => {
});

it('should set the group name as the first selected status', () => {
component.groupForm.patchValue({name: undefined, statuses: []});
component.groupForm.patchValue({name: null, color: null, statuses: []});
const event = {
checked: true,
source: {
Expand All @@ -101,35 +122,35 @@ describe('FormStatusesGroupComponent', () => {
});
});

it('should emit on submit', () => {
const newGroup = {
name: 'name',
color: 'green',
statuses: [TaskStatus.TASK_STATUS_UNSPECIFIED, TaskStatus.TASK_STATUS_CREATING]
};
component.groupForm.setValue(newGroup);

const spySubmit = jest.spyOn(component.submitChange, 'emit');
component.onSubmit();

expect(spySubmit).toHaveBeenCalledWith(newGroup);
it('should get a status label', () => {
expect(component.getLabel(`${TaskStatus.TASK_STATUS_COMPLETED}`)).toEqual(statusesLabelsColors[TaskStatus.TASK_STATUS_COMPLETED].label);
});

it('should emit on submit even without values', () => {
const undefinedGroup = {
name: null,
color: null,
statuses: null
};
component.groupForm.setValue(undefinedGroup);

const spySubmit = jest.spyOn(component.submitChange, 'emit');

component.onSubmit();
expect(spySubmit).toHaveBeenCalledWith({
name: '',
color: '',
statuses: []
describe('Submitting', () => {
it('should emit', () => {
const spySubmit = jest.spyOn(component.submitChange, 'emit');
component.onSubmit();

expect(spySubmit).toHaveBeenCalledWith(defaultGroup);
});

it('should emit even without values', () => {
const undefinedGroup = {
name: null,
color: null,
statuses: null
};
component.groupForm.setValue(undefinedGroup);

const spySubmit = jest.spyOn(component.submitChange, 'emit');

component.onSubmit();
expect(spySubmit).toHaveBeenCalledWith({
name: '',
color: '',
statuses: []
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('ManageGroupsDialogComponent', () => {

it('should get statuses labels', () => {
expect(component.statusToLabel(TaskStatus.TASK_STATUS_COMPLETED)).toEqual({
color: 'darkgreen',
color: '#006400',
icon: 'success',
label: 'Completed'
});
Expand Down

0 comments on commit 7f5f153

Please sign in to comment.