| /******************************************************************************** |
| * Copyright (c) 2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0 |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ********************************************************************************/ |
| |
| import {Component} from "@angular/core"; |
| import {ComponentFixture, TestBed} from "@angular/core/testing"; |
| import {By} from "@angular/platform-browser"; |
| import {IAPIAttachmentModel} from "../../../../core/api"; |
| import {I18nModule} from "../../../../core/i18n"; |
| import {IAttachmentWithTags} from "../../../../store/attachments/model"; |
| import {FileSelectModule} from "../file-select.module"; |
| import {FileSelectComponent} from "./file-select.component"; |
| |
| @Component({ |
| selector: `app-host-component`, |
| template: ` |
| <app-file-select |
| [appAttachments]="attachments" |
| [appValue]="appValue"> |
| </app-file-select> |
| ` |
| }) |
| class TestHostComponent { |
| |
| public attachments: IAPIAttachmentModel[] = [ |
| { |
| id: 1, |
| name: "Attachment 1", |
| type: "string", |
| size: 1, |
| timestamp: "string", |
| tagIds: [] |
| }, |
| { |
| id: 2, |
| name: "Attachment 1", |
| type: "string", |
| size: 1, |
| timestamp: "string", |
| tagIds: [] |
| } |
| ]; |
| |
| public appValue: IAttachmentWithTags<number>[] = [ |
| { |
| attachment: 2, |
| tags: [] |
| } |
| ]; |
| } |
| |
| describe("FileSelectComponent", () => { |
| let component: TestHostComponent; |
| let fixture: ComponentFixture<TestHostComponent>; |
| let childComponent: FileSelectComponent; |
| |
| beforeEach(async () => { |
| await TestBed.configureTestingModule({ |
| declarations: [ |
| TestHostComponent |
| ], |
| imports: [ |
| FileSelectModule, |
| I18nModule |
| ] |
| }).compileComponents(); |
| }); |
| |
| beforeEach(() => { |
| fixture = TestBed.createComponent(TestHostComponent); |
| component = fixture.componentInstance; |
| childComponent = fixture.debugElement.query(By.directive(FileSelectComponent)).componentInstance; |
| fixture.detectChanges(); |
| }); |
| |
| it("should create", () => { |
| expect(component).toBeTruthy(); |
| }); |
| |
| it("should emit appOpenAttachment with the file id of the given file", () => { |
| spyOn(childComponent.appOpenAttachment, "emit").and.callThrough(); |
| const button = fixture.debugElement.query(By.css(".openk-button")); |
| button.nativeElement.click(); |
| expect(childComponent.appOpenAttachment.emit).toHaveBeenCalledWith(1); |
| }); |
| |
| it("should return true only if the file id is not in appValue", () => { |
| const isSelected = childComponent.isSelected(childComponent.appAttachments[0].id); |
| expect(isSelected).toBeTrue(); |
| const isNotSelected = childComponent.isSelected(childComponent.appAttachments[1].id); |
| expect(isNotSelected).toBeFalse(); |
| }); |
| |
| it("should remove the file id from app value on select", () => { |
| childComponent.appValue = getAttachmentListWithTags([2, 3]); |
| childComponent.select(2); |
| expect(childComponent.appValue).toEqual(getAttachmentListWithTags([3])); |
| childComponent.select(3); |
| expect(childComponent.appValue).toEqual([]); |
| }); |
| |
| it("should add the file id to app value on deselect", () => { |
| childComponent.appValue = []; |
| childComponent.deselect(2); |
| expect(childComponent.appValue).toEqual(getAttachmentListWithTags([2])); |
| childComponent.deselect(3); |
| expect(childComponent.appValue).toEqual(getAttachmentListWithTags([2, 3])); |
| }); |
| |
| it("should add and remove items from appValue when no input value was given", () => { |
| childComponent.appValue = undefined; |
| childComponent.select(2); |
| expect(childComponent.appValue).toEqual([]); |
| childComponent.appValue = undefined; |
| childComponent.deselect(3); |
| expect(childComponent.appValue).toEqual(getAttachmentListWithTags([3])); |
| }); |
| }); |
| |
| const getAttachmentWithTags = (attachment: number) => { |
| return { |
| attachment, |
| tags: [], |
| remove: true |
| }; |
| }; |
| |
| const getAttachmentListWithTags = (attachments: any[]) => { |
| return attachments.map((_) => getAttachmentWithTags(_)); |
| }; |