From 1e53d19639a4c57e4de5b2937ea99e7715140cec Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Fri, 9 Feb 2018 12:28:52 -0600 Subject: [PATCH] test(init): ensure componentDidLoad called once --- src/core/instance/test/init.spec.ts | 52 ++++++++++++++++++----------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/core/instance/test/init.spec.ts b/src/core/instance/test/init.spec.ts index 3fd11c184c0..159f074547b 100644 --- a/src/core/instance/test/init.spec.ts +++ b/src/core/instance/test/init.spec.ts @@ -1,11 +1,31 @@ -import { mockPlatform, mockDomApi } from '../../../testing/mocks'; -import { ComponentInstance, ComponentMeta, HostElement, PlatformApi } from '../../../util/interfaces'; +import { ComponentInstance, ComponentMeta, HostElement, PlatformApi } from '../../../declarations'; import { initHostElement } from '../init-host-element'; import { initComponentLoaded } from '../init-component-instance'; +import { mockDomApi, mockPlatform } from '../../../testing/mocks'; describe('instance init', () => { + const plt: PlatformApi = mockPlatform(); + const domApi = mockDomApi(); + let elm: HostElement; + let instance: ComponentInstance; + let cmpMeta: ComponentMeta; + + class TestInstance { + state = 'value'; + componentDidLoad() {/**/} + } + + beforeEach(() => { + cmpMeta = {}; + elm = domApi.$createElement('ion-cmp') as any; + instance = new TestInstance(); + elm._instance = instance; + instance.__el = elm; + }); + + describe('initLoad', () => { it('should call multiple componentOnReady promises', () => { @@ -14,11 +34,11 @@ describe('instance init', () => { let called1 = false; let called2 = false; - let p1 = elm.componentOnReady().then(() => { + const p1 = elm.componentOnReady().then(() => { called1 = true; }); - let p2 = elm.componentOnReady().then(() => { + const p2 = elm.componentOnReady().then(() => { called2 = true; }); @@ -48,24 +68,18 @@ describe('instance init', () => { expect(called2).toBe(true); }); - }); + it('should not call componentDidLoad() more than once', () => { + initHostElement(plt, cmpMeta, elm); - const plt: PlatformApi = mockPlatform(); - const domApi = mockDomApi(); - let elm: HostElement; - let instance: ComponentInstance; - let cmpMeta: ComponentMeta; + const spy = spyOn(instance, 'componentDidLoad'); - class TestInstance { - state = 'value'; - } + initComponentLoaded(plt, elm); + expect(spy).toHaveBeenCalledTimes(1); + + initComponentLoaded(plt, elm); + expect(spy).toHaveBeenCalledTimes(1); + }); - beforeEach(() => { - cmpMeta = {}; - elm = domApi.$createElement('ion-cmp') as any; - instance = new TestInstance(); - elm._instance = instance; - instance.__el = elm; }); });