From 12442b4871bb32c3271566de9ec790c69a41e2c0 Mon Sep 17 00:00:00 2001 From: Alex Ryltsov Date: Sat, 3 Aug 2019 09:19:30 +0300 Subject: [PATCH] Enabled support for the lookup items having id (unique identifier) value as 0 (zero) --- package.json | 2 +- .../multi-level-select-dialog.spec.ts | 335 ++++++++++++------ src/components/multi-level-select-dialog.ts | 2 +- 3 files changed, 236 insertions(+), 103 deletions(-) diff --git a/package.json b/package.json index 4980292..3db404a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "ionic3-multi-level-select", "author": "Alex Ryltsov", "homepage": "https://github.com/ryaa/ionic3-multi-level-select", - "version": "0.0.3", + "version": "0.0.4", "description": "Ionic multi level select module allowing to display hierarchical data in a mobile friendly manner.", "license": "MIT", "main": "./dist/index.js", diff --git a/src/components/multi-level-select-dialog.spec.ts b/src/components/multi-level-select-dialog.spec.ts index 75d3f16..aa5fd3c 100644 --- a/src/components/multi-level-select-dialog.spec.ts +++ b/src/components/multi-level-select-dialog.spec.ts @@ -1151,133 +1151,266 @@ describe('MultiLevelSelectDialogComponent:', () => { }); describe('when itemOrIndex is defined', () => { describe('when itemOrIndex param is SlideItem', () => { - beforeEach(() => { - itemOrIndexParam = { - id: 1, - parentId: null, - nextSlideIndex: null, - slide: 'slide' as any, - lookUpItem: 'lookUpItem' as any - }; - }); - describe('when itemOrIndex.nextSlideIndex is defined', () => { + describe('and its id is 1', () => { beforeEach(() => { - (itemOrIndexParam as SlideItem).nextSlideIndex = 555; + itemOrIndexParam = { + id: 1, + parentId: null, + nextSlideIndex: null, + slide: 'slide' as any, + lookUpItem: 'lookUpItem' as any + }; }); - it('should NOT invoke addChildren', (done) => { - fakeAsync(() => { - comp.slideTo(itemOrIndexParam); - tick(delay); + describe('when itemOrIndex.nextSlideIndex is defined', () => { + beforeEach(() => { + (itemOrIndexParam as SlideItem).nextSlideIndex = 555; + }); + it('should NOT invoke addChildren', (done) => { + fakeAsync(() => { + comp.slideTo(itemOrIndexParam); + tick(delay); - expect(comp.addChildren).not.toHaveBeenCalled(); - done(); - })(); - }); - it('should invoke slidesControl.lockSwipes twice with the right params', (done) => { - fakeAsync(() => { - comp.slideTo(itemOrIndexParam); - tick(delay); + expect(comp.addChildren).not.toHaveBeenCalled(); + done(); + })(); + }); + it('should invoke slidesControl.lockSwipes twice with the right params', (done) => { + fakeAsync(() => { + comp.slideTo(itemOrIndexParam); + tick(delay); - expect(comp.slidesControl.lockSwipes).toHaveBeenCalledTimes(2); - expect((comp.slidesControl.lockSwipes as any).calls.argsFor(0)).toEqual([false]); - expect((comp.slidesControl.lockSwipes as any).calls.argsFor(1)).toEqual([true]); - done(); - })(); - }); - it('should invoke slidesControl.slideTo with the right params', (done) => { - fakeAsync(() => { - comp.slideTo(itemOrIndexParam); - tick(delay); + expect(comp.slidesControl.lockSwipes).toHaveBeenCalledTimes(2); + expect((comp.slidesControl.lockSwipes as any).calls.argsFor(0)).toEqual([false]); + expect((comp.slidesControl.lockSwipes as any).calls.argsFor(1)).toEqual([true]); + done(); + })(); + }); + it('should invoke slidesControl.slideTo with the right params', (done) => { + fakeAsync(() => { + comp.slideTo(itemOrIndexParam); + tick(delay); - expect(comp.slidesControl.slideTo).toHaveBeenCalledTimes(1); - expect(comp.slidesControl.slideTo).toHaveBeenCalledWith(555); - done(); - })(); - }); - }); - describe('when itemOrIndex.nextSlideIndex is undefined', () => { - beforeEach(() => { - (itemOrIndexParam as SlideItem).nextSlideIndex = undefined; + expect(comp.slidesControl.slideTo).toHaveBeenCalledTimes(1); + expect(comp.slidesControl.slideTo).toHaveBeenCalledWith(555); + done(); + })(); + }); }); - it('should invoke addChildren with the right params', (done) => { - fakeAsync(() => { - comp.slideTo(itemOrIndexParam); - tick(delay); + describe('when itemOrIndex.nextSlideIndex is undefined', () => { + beforeEach(() => { + (itemOrIndexParam as SlideItem).nextSlideIndex = undefined; + }); + it('should invoke addChildren with the right params', (done) => { + fakeAsync(() => { + comp.slideTo(itemOrIndexParam); + tick(delay); - expect(comp.addChildren).toHaveBeenCalledTimes(1); - expect(comp.addChildren).toHaveBeenCalledWith( - (itemOrIndexParam as SlideItem).slide, (itemOrIndexParam as SlideItem), (itemOrIndexParam as SlideItem).lookUpItem.children - ); - done(); - })(); - }); - it('should invoke slidesControl.lockSwipes twice with the right params', (done) => { - fakeAsync(() => { - comp.slideTo(itemOrIndexParam); - tick(delay); + expect(comp.addChildren).toHaveBeenCalledTimes(1); + expect(comp.addChildren).toHaveBeenCalledWith( + (itemOrIndexParam as SlideItem).slide, (itemOrIndexParam as SlideItem), (itemOrIndexParam as SlideItem).lookUpItem.children + ); + done(); + })(); + }); + it('should invoke slidesControl.lockSwipes twice with the right params', (done) => { + fakeAsync(() => { + comp.slideTo(itemOrIndexParam); + tick(delay); - expect(comp.slidesControl.lockSwipes).toHaveBeenCalledTimes(2); - expect((comp.slidesControl.lockSwipes as any).calls.argsFor(0)).toEqual([false]); - expect((comp.slidesControl.lockSwipes as any).calls.argsFor(1)).toEqual([true]); - done(); - })(); - }); - it('should invoke slidesControl.slideTo with the right params', (done) => { - fakeAsync(() => { - comp.slideTo(itemOrIndexParam); - tick(delay); + expect(comp.slidesControl.lockSwipes).toHaveBeenCalledTimes(2); + expect((comp.slidesControl.lockSwipes as any).calls.argsFor(0)).toEqual([false]); + expect((comp.slidesControl.lockSwipes as any).calls.argsFor(1)).toEqual([true]); + done(); + })(); + }); + it('should invoke slidesControl.slideTo with the right params', (done) => { + fakeAsync(() => { + comp.slideTo(itemOrIndexParam); + tick(delay); - expect(comp.slidesControl.slideTo).toHaveBeenCalledTimes(1); - expect(comp.slidesControl.slideTo).toHaveBeenCalledWith('nextSlideIndexSetInAddChildren' as any); - done(); - })(); + expect(comp.slidesControl.slideTo).toHaveBeenCalledTimes(1); + expect(comp.slidesControl.slideTo).toHaveBeenCalledWith('nextSlideIndexSetInAddChildren' as any); + done(); + })(); + }); + }); + describe('when itemOrIndex.nextSlideIndex is null', () => { + beforeEach(() => { + (itemOrIndexParam as SlideItem).nextSlideIndex = null; + }); + it('should NOT invoke addChildren', (done) => { + fakeAsync(() => { + try { + comp.slideTo(itemOrIndexParam); + tick(delay); + } catch (error) { + expect(comp.addChildren).not.toHaveBeenCalled(); + done(); + } + })(); + }); + it('should NOT invoke slidesControl.lockSwipes', (done) => { + fakeAsync(() => { + try { + comp.slideTo(itemOrIndexParam); + tick(delay); + } catch (error) { + expect(comp.slidesControl.lockSwipes).not.toHaveBeenCalled(); + done(); + } + })(); + }); + it('should NOT invoke slidesControl.slideTo', (done) => { + fakeAsync(() => { + try { + comp.slideTo(itemOrIndexParam); + tick(delay); + } catch (error) { + expect(comp.slidesControl.slideTo).not.toHaveBeenCalled(); + done(); + } + })(); + }); + it('should throw the right error', (done) => { + fakeAsync(() => { + expect(() => { + comp.slideTo(itemOrIndexParam); + tick(delay); + }).toThrowError(`MultiLevelSelectDialogComponent: itemOrIndex.nextSlideIndex must not be null; itemOrIndex: ${JSON.stringify(itemOrIndexParam)}`); + done(); + })(); + }); }); }); - describe('when itemOrIndex.nextSlideIndex is null', () => { + describe('and its id is 0', () => { beforeEach(() => { - (itemOrIndexParam as SlideItem).nextSlideIndex = null; + itemOrIndexParam = { + id: 0, + parentId: null, + nextSlideIndex: null, + slide: 'slide' as any, + lookUpItem: 'lookUpItem' as any + }; }); - it('should NOT invoke addChildren', (done) => { - fakeAsync(() => { - try { + describe('when itemOrIndex.nextSlideIndex is defined', () => { + beforeEach(() => { + (itemOrIndexParam as SlideItem).nextSlideIndex = 555; + }); + it('should NOT invoke addChildren', (done) => { + fakeAsync(() => { comp.slideTo(itemOrIndexParam); tick(delay); - } catch (error) { + expect(comp.addChildren).not.toHaveBeenCalled(); done(); - } - })(); - }); - it('should NOT invoke slidesControl.lockSwipes', (done) => { - fakeAsync(() => { - try { + })(); + }); + it('should invoke slidesControl.lockSwipes twice with the right params', (done) => { + fakeAsync(() => { comp.slideTo(itemOrIndexParam); tick(delay); - } catch (error) { - expect(comp.slidesControl.lockSwipes).not.toHaveBeenCalled(); + + expect(comp.slidesControl.lockSwipes).toHaveBeenCalledTimes(2); + expect((comp.slidesControl.lockSwipes as any).calls.argsFor(0)).toEqual([false]); + expect((comp.slidesControl.lockSwipes as any).calls.argsFor(1)).toEqual([true]); done(); - } - })(); - }); - it('should NOT invoke slidesControl.slideTo', (done) => { - fakeAsync(() => { - try { + })(); + }); + it('should invoke slidesControl.slideTo with the right params', (done) => { + fakeAsync(() => { comp.slideTo(itemOrIndexParam); tick(delay); - } catch (error) { - expect(comp.slidesControl.slideTo).not.toHaveBeenCalled(); + + expect(comp.slidesControl.slideTo).toHaveBeenCalledTimes(1); + expect(comp.slidesControl.slideTo).toHaveBeenCalledWith(555); done(); - } - })(); + })(); + }); }); - it('should throw the right error', (done) => { - fakeAsync(() => { - expect(() => { + describe('when itemOrIndex.nextSlideIndex is undefined', () => { + beforeEach(() => { + (itemOrIndexParam as SlideItem).nextSlideIndex = undefined; + }); + it('should invoke addChildren with the right params', (done) => { + fakeAsync(() => { comp.slideTo(itemOrIndexParam); tick(delay); - }).toThrowError(`MultiLevelSelectDialogComponent: itemOrIndex.nextSlideIndex must not be null; itemOrIndex: ${JSON.stringify(itemOrIndexParam)}`); - done(); - })(); + + expect(comp.addChildren).toHaveBeenCalledTimes(1); + expect(comp.addChildren).toHaveBeenCalledWith( + (itemOrIndexParam as SlideItem).slide, (itemOrIndexParam as SlideItem), (itemOrIndexParam as SlideItem).lookUpItem.children + ); + done(); + })(); + }); + it('should invoke slidesControl.lockSwipes twice with the right params', (done) => { + fakeAsync(() => { + comp.slideTo(itemOrIndexParam); + tick(delay); + + expect(comp.slidesControl.lockSwipes).toHaveBeenCalledTimes(2); + expect((comp.slidesControl.lockSwipes as any).calls.argsFor(0)).toEqual([false]); + expect((comp.slidesControl.lockSwipes as any).calls.argsFor(1)).toEqual([true]); + done(); + })(); + }); + it('should invoke slidesControl.slideTo with the right params', (done) => { + fakeAsync(() => { + comp.slideTo(itemOrIndexParam); + tick(delay); + + expect(comp.slidesControl.slideTo).toHaveBeenCalledTimes(1); + expect(comp.slidesControl.slideTo).toHaveBeenCalledWith('nextSlideIndexSetInAddChildren' as any); + done(); + })(); + }); + }); + describe('when itemOrIndex.nextSlideIndex is null', () => { + beforeEach(() => { + (itemOrIndexParam as SlideItem).nextSlideIndex = null; + }); + it('should NOT invoke addChildren', (done) => { + fakeAsync(() => { + try { + comp.slideTo(itemOrIndexParam); + tick(delay); + } catch (error) { + expect(comp.addChildren).not.toHaveBeenCalled(); + done(); + } + })(); + }); + it('should NOT invoke slidesControl.lockSwipes', (done) => { + fakeAsync(() => { + try { + comp.slideTo(itemOrIndexParam); + tick(delay); + } catch (error) { + expect(comp.slidesControl.lockSwipes).not.toHaveBeenCalled(); + done(); + } + })(); + }); + it('should NOT invoke slidesControl.slideTo', (done) => { + fakeAsync(() => { + try { + comp.slideTo(itemOrIndexParam); + tick(delay); + } catch (error) { + expect(comp.slidesControl.slideTo).not.toHaveBeenCalled(); + done(); + } + })(); + }); + it('should throw the right error', (done) => { + fakeAsync(() => { + expect(() => { + comp.slideTo(itemOrIndexParam); + tick(delay); + }).toThrowError(`MultiLevelSelectDialogComponent: itemOrIndex.nextSlideIndex must not be null; itemOrIndex: ${JSON.stringify(itemOrIndexParam)}`); + done(); + })(); + }); }); }); }); diff --git a/src/components/multi-level-select-dialog.ts b/src/components/multi-level-select-dialog.ts index f6cea91..dd4059f 100644 --- a/src/components/multi-level-select-dialog.ts +++ b/src/components/multi-level-select-dialog.ts @@ -269,7 +269,7 @@ export class MultiLevelSelectDialogComponent { public slideTo(itemOrIndex: SlideItem | number): void { if (itemOrIndex != null) { let nextSlideIndex = null; - if ((itemOrIndex as SlideItem).id) { + if ((itemOrIndex as SlideItem).id != null) { if ((itemOrIndex as SlideItem).nextSlideIndex != null) { nextSlideIndex = (itemOrIndex as SlideItem).nextSlideIndex; } else {