| /******************************************************************************** |
| * 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 {ObjToArrayPipe} from "./obj-to-array.pipe"; |
| |
| describe("ObjToArrayPipe", () => { |
| |
| describe("pipe", () => { |
| |
| @Component({ |
| template: `<span *ngFor="let entry of obj | objToArray: keep"> |
| {{entry.key}}: {{entry.value}} |
| </span>` |
| }) |
| class TestComponent { |
| obj; |
| keep: boolean; |
| } |
| |
| let component: TestComponent; |
| let fixture: ComponentFixture<TestComponent>; |
| let el: HTMLElement; |
| |
| beforeEach(() => { |
| TestBed.configureTestingModule({ |
| declarations: [ |
| ObjToArrayPipe, |
| TestComponent |
| ] |
| }); |
| fixture = TestBed.createComponent(TestComponent); |
| component = fixture.componentInstance; |
| el = fixture.nativeElement; |
| fixture.detectChanges(); |
| }); |
| |
| it("should convert the input object to an array, removing null and undefined", () => { |
| component.obj = { |
| prop1: "propValue1", |
| prop2: 2, |
| 3: 4, |
| prop4: null |
| }; |
| fixture.detectChanges(); |
| const listEntriesInHtml = el.querySelectorAll("span"); |
| expect(listEntriesInHtml.length).toBe(Object.keys(component.obj).length - 1); |
| const spans = Array.from(el.querySelectorAll("span")); |
| expect(spans.find((span) => span.textContent.includes("3: 4"))).toBeTruthy(); |
| expect(spans.find((span) => span.textContent.includes("prop1: propValue1"))).toBeTruthy(); |
| expect(spans.find((span) => span.textContent.includes("prop2: 2"))).toBeTruthy(); |
| }); |
| |
| it("should convert the input object to an array, keeping null and undefined", () => { |
| component.obj = { |
| prop1: "propValue1", |
| prop2: 2, |
| 3: 4, |
| prop4: null |
| }; |
| component.keep = true; |
| fixture.detectChanges(); |
| const listEntriesInHtml = el.querySelectorAll("span"); |
| expect(listEntriesInHtml.length).toBe(Object.keys(component.obj).length); |
| const spans = Array.from(el.querySelectorAll("span")); |
| expect(spans.find((span) => span.textContent.includes("3: 4"))).toBeTruthy(); |
| expect(spans.find((span) => span.textContent.includes("prop1: propValue1"))).toBeTruthy(); |
| expect(spans.find((span) => span.textContent.includes("prop2: 2"))).toBeTruthy(); |
| }); |
| }); |
| |
| describe("transform", () => { |
| |
| const pipe = new ObjToArrayPipe(); |
| |
| it("should convert a object to an array", () => { |
| const testObject = { |
| prop1: "propValue1", |
| prop2: "propValue2", |
| prop3: 3 |
| }; |
| const expectedArray: Array<{ key: number | string, value: any }> = [ |
| {key: "prop1", value: "propValue1"}, |
| {key: "prop2", value: "propValue2"}, |
| {key: "prop3", value: 3} |
| ]; |
| const array = pipe.transform(testObject); |
| expect(Array.isArray(array)).toBe(true); |
| expect(array).toEqual(expectedArray); |
| }); |
| |
| it("should return empty array for null/undefined", () => { |
| const undefinedToArray = pipe.transform(undefined); |
| expect(Array.isArray(undefinedToArray)).toBeTrue(); |
| expect(undefinedToArray.length).toBe(0); |
| const nullToArray = pipe.transform(undefined); |
| expect(Array.isArray(nullToArray)).toBeTrue(); |
| expect(nullToArray.length).toBe(0); |
| }); |
| |
| it("should not keep null or undefined properties (default)", () => { |
| const testObject = { |
| prop1: "propValue1", |
| prop2: undefined, |
| prop3: null |
| }; |
| const expectedArray: Array<{ key: number | string, value: any }> = [ |
| {key: "prop1", value: "propValue1"}, |
| ]; |
| let array = pipe.transform(testObject); |
| expect(Array.isArray(array)).toBe(true); |
| expect(array).toEqual(expectedArray); |
| array = pipe.transform(testObject, false); |
| expect(Array.isArray(array)).toBe(true); |
| expect(array).toEqual(expectedArray); |
| }); |
| |
| it("should keep null or undefined properties", () => { |
| const testObject = { |
| prop1: "propValue1", |
| prop2: undefined, |
| prop3: null |
| }; |
| const expectedArray: Array<{ key: number | string, value: any }> = [ |
| {key: "prop1", value: "propValue1"}, |
| {key: "prop2", value: undefined}, |
| {key: "prop3", value: null}, |
| ]; |
| const array = pipe.transform(testObject, true); |
| expect(Array.isArray(array)).toBe(true); |
| expect(array).toEqual(expectedArray); |
| }); |
| }); |
| }); |