Skip to content

Commit

Permalink
de-duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
clemiller committed Feb 3, 2024
1 parent 6cebf22 commit d73d20d
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 371 deletions.
154 changes: 59 additions & 95 deletions nav-app/src/app/list-input/list-input.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ListInputComponent } from './list-input.component';
import { Link, Metadata, ViewModel } from '../classes';
import * as MockData from '../../tests/utils/mock-data';

describe('ListInputComponent', () => {
let component: ListInputComponent;
let fixture: ComponentFixture<ListInputComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ListInputComponent],
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(ListInputComponent);
component = fixture.debugElement.componentInstance;
Expand Down Expand Up @@ -46,120 +41,89 @@ describe('ListInputComponent', () => {
expect(component.list.length).toEqual(1);
});

it('should add and remove divider for layer', () => {
let vm1 = new ViewModel("layer","33","enterprise-attack-13",null);
let m1 = new Metadata();
m1.name = "test1";
m1.value = "t1";
vm1.metadata.push(m1);
let m2 = new Metadata();
m2.name = "test2";
m2.value = "t2";
vm1.metadata.push(m2);
component.config = {
viewModel: vm1,
list: vm1.metadata,
level: 'layer',
type: 'metadata',
nameField: 'name',
valueField: 'value',
};
component.ngOnInit();
if (component.canAddDivider(1)) {
component.addDivider(1);
}
component.addDivider(1);
component.removeDivider(1);
expect(component.list.length).toEqual(3);
});

it('should throw errors for metadata', () => {
let m1 = new Metadata();
let metatdata_error_file1 = {
"name":3,
"value":4
}
let metatdata_error_file2 = {
"name":3
}
let metatdata_error_file3 = {
"divider":"test1"
}
let metatdata_error_file4 = {
"value":"test1"
}
let consoleSpy = spyOn(console, 'error');
m1.deserialize(JSON.stringify(metatdata_error_file1));
let metadata = new Metadata();
metadata.deserialize(JSON.stringify(MockData.invalidMetadata));
expect(consoleSpy).toHaveBeenCalledWith('TypeError: Metadata field \'name\' is not a string');
expect(consoleSpy).toHaveBeenCalledWith('TypeError: Metadata field \'value\' is not a string');
m1.deserialize(JSON.stringify(metatdata_error_file2));
metadata.deserialize(JSON.stringify(MockData.invalidName));
expect(consoleSpy).toHaveBeenCalledWith('Error: Metadata required field \'value\' not present');
m1.deserialize(JSON.stringify(metatdata_error_file3));
metadata.deserialize(JSON.stringify(MockData.invalidDivider));
expect(consoleSpy).toHaveBeenCalledWith('TypeError: Metadata field \'divider\' is not a boolean');
m1.deserialize(JSON.stringify(metatdata_error_file4));
metadata.deserialize(JSON.stringify(MockData.invalidValue));
expect(consoleSpy).toHaveBeenCalledWith('Error: Metadata required field \'name\' or \'divider\' not present');
})

it('should throw errors for links', () => {
let l1 = new Link();
let link_error_file1 = {
"url":3,
"label":4
}
let link_error_file2 = {
"url":3
}
let link_error_file3 = {
"divider":"test1"
}
let link_error_file4 = {
"value":"test1"
}
let consoleSpy = spyOn(console, 'error');
l1.deserialize(JSON.stringify(link_error_file1));
let link = new Link();
link.deserialize(JSON.stringify(MockData.invalidLink));
expect(consoleSpy).toHaveBeenCalledWith('TypeError: Link field \'url\' is not a string');
expect(consoleSpy).toHaveBeenCalledWith('TypeError: Link field \'label\' is not a string');
l1.deserialize(JSON.stringify(link_error_file2));
link.deserialize(JSON.stringify(MockData.invalidUrl));
expect(consoleSpy).toHaveBeenCalledWith('Error: Link required field \'label\' not present');
l1.deserialize(JSON.stringify(link_error_file3));
link.deserialize(JSON.stringify(MockData.invalidDivider));
expect(consoleSpy).toHaveBeenCalledWith('TypeError: Link field \'divider\' is not a boolean');
l1.deserialize(JSON.stringify(link_error_file4));
link.deserialize(JSON.stringify(MockData.invalidValue));
expect(consoleSpy).toHaveBeenCalledWith('Error: Link required field \'url\' or \'divider\' not present');
})
});

it('should not add divider', () => {
expect(component.canAddDivider(5)).toEqual(false);
expect(component.canAddDivider(0)).toEqual(false);
it('should return false if links are not in config type', () => {
expect(component.includeLinks).toEqual(false);
});
});

it('should add and remove divider for technique', () => {
let vm1 = new ViewModel("layer","33","enterprise-attack-13",null);
let m1 = new Metadata();
m1.name = "test1";
m1.value = "t1";
vm1.metadata.push(m1);
let m2 = new Metadata();
m2.name = "test2";
m2.value = "t2";
vm1.metadata.push(m2);
describe('Dividers', () => {
let component: ListInputComponent;
let fixture: ComponentFixture<ListInputComponent>;
let viewModel: ViewModel;

let addDivider = (component) => {
if (component.canAddDivider(1)) {
component.addDivider(1);
}
component.addDivider(1);
component.removeDivider(1);
}

beforeEach(() => {
fixture = TestBed.createComponent(ListInputComponent);
component = fixture.debugElement.componentInstance;
viewModel = new ViewModel("layer", "1", "enterprise-attack-13", null);
let metadata = new Metadata();
metadata.name = "test1";
metadata.value = "t1";
let metadata2 = new Metadata();
metadata2.name = "test2";
metadata2.value = "t2";
viewModel.metadata = [metadata, metadata2];
component.config = {
viewModel: vm1,
list: vm1.metadata,
level: 'technique',
viewModel: viewModel,
list: viewModel.metadata,
level: 'layer',
type: 'metadata',
nameField: 'name',
valueField: 'value',
};
component.ngOnInit();
if (component.canAddDivider(1)) {
component.addDivider(1);
}
component.addDivider(1);
component.removeDivider(1);
component.ngOnInit();
fixture.detectChanges();
});

it('should add and remove divider for layer', () => {
component.config.level = 'layer';
addDivider(component);
expect(component.list.length).toEqual(3);
});

it('should return false if links are not in config type', () => {
expect(component.includeLinks).toEqual(false);
it('should add and remove divider for technique', () => {
component.config.level = 'technique';
addDivider(component);
expect(component.list.length).toEqual(3);
});
});

it('should not add divider', () => {
expect(component.canAddDivider(5)).toEqual(false);
expect(component.canAddDivider(0)).toEqual(false);
});
});
Loading

0 comments on commit d73d20d

Please sign in to comment.