From abffd3bee3d61dc37225765921a35ec205382eb2 Mon Sep 17 00:00:00 2001 From: Michael Kurze Date: Mon, 22 Aug 2016 09:57:19 +0200 Subject: [PATCH] (#28) fixed mock injections for axAssets and axConfiguration --- CHANGELOG.md | 1 + laxar-mocks.js | 22 +++++++++++++-------- spec/laxar-mocks.spec.js | 42 ++++++++++++++++++++++++++++------------ 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d55e2fc..39a4f2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Last Changes +- [#28](https://github.com/LaxarJS/laxar-mocks/issues/28): fixed mock injections for axAssets and axConfiguration - [#27](https://github.com/LaxarJS/laxar-mocks/issues/27): updated for LaxarJS v2.0 compatibility (laxar#358) diff --git a/laxar-mocks.js b/laxar-mocks.js index 69efae7..a944c54 100644 --- a/laxar-mocks.js +++ b/laxar-mocks.js @@ -33,10 +33,6 @@ const nextTick = f => { const noOp = () => {}; -let laxarServices; -let anchorElement; -let adapterInstance; - ////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** @@ -54,6 +50,13 @@ export const fixtures = {}; export let eventBus; +let adapterInstance; +let adapter; +let anchorElement; +let artifacts; +let configuration; +let laxarServices; + ////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** @@ -180,8 +183,11 @@ function decoratedAdapter( adapter ) { return function serviceDecorators() { return { ...( adapterFactory.serviceDecorators || noOp )(), - axAssets: () => createAxAssetsMock( /* TODO pass artifacts listing */ ), - axConfiguration: () => createAxConfigurationMock( /* TODO pass configuration data */ ), + axAssets: () => { + const { assets } = artifacts.widgets[ 0 ]; + return createAxAssetsMock( assets ); + }, + axConfiguration: () => createAxConfigurationMock( configuration ), axEventBus: eventBus => { const methods = [ 'subscribe', 'publish', 'publishAndGatherReplies', 'addInspector' ]; methods.forEach( method => { @@ -415,10 +421,10 @@ export function triggerStartupEvents( optionalEvents = {} ) { */ export function createSetupForWidget( descriptor, optionalOptions = {} ) { return done => { - const { artifacts = {}, adapter = plainAdapter, configuration = {} } = { + ({ artifacts = {}, adapter = plainAdapter, configuration = {} } = { ...fixtures[ descriptor.name ], ...optionalOptions - }; + }); let htmlTemplate; let features = {}; diff --git a/spec/laxar-mocks.spec.js b/spec/laxar-mocks.spec.js index 708e429..59e304e 100644 --- a/spec/laxar-mocks.spec.js +++ b/spec/laxar-mocks.spec.js @@ -18,11 +18,7 @@ describe( 'A laxar-mocks test runner', () => { beforeEach( done => { descriptor = createFakeDescriptor( 'some-activity', 'activity' ); create = jasmine.createSpy( 'widgetModule.create' ); - artifacts = createFakeArtifacts( descriptor, { - create, - // TODO (after laxar#358): remove - name: 'some-activity' - } ); + artifacts = createFakeArtifacts( descriptor, { create } ); configuration = { baseHref: '/' }; axMocks.createSetupForWidget( descriptor, { artifacts, configuration } )( done ); } ); @@ -77,10 +73,8 @@ describe( 'A laxar-mocks test runner', () => { onDomAvailable: () => {} }) ); artifacts = createFakeArtifacts( descriptor, { - injections: [ 'axFeatures', 'axStorage', 'axLog' ], - create, - // TODO (after laxar#358): remove - name: 'some-widget' + injections: [ 'axFeatures', 'axStorage', 'axLog', 'axConfiguration', 'axAssets' ], + create } ); configuration = { baseHref: '/' }; axMocks.createSetupForWidget( descriptor, { artifacts, configuration } )( done ); @@ -99,17 +93,19 @@ describe( 'A laxar-mocks test runner', () => { it( 'instantiates the widget controller with the requested injections', () => { expect( create ).toHaveBeenCalled(); - const [ features, storage, log ] = create.calls.mostRecent().args; + const [ features, storage, log, configuration, assets ] = create.calls.mostRecent().args; expect( features ).toEqual( { someFeature: 'someValue', other: 'default' } ); expect( storage.local.setItem ).toEqual( jasmine.any( Function ) ); expect( log.info ).toEqual( jasmine.any( Function ) ); + expect( configuration.get ).toEqual( jasmine.any( Function ) ); + expect( assets ).toEqual( jasmine.any( Function ) ); } ); ///////////////////////////////////////////////////////////////////////////////////////////////////// it( 'provides a mock-storage injection without side-effects', () => { expect( create ).toHaveBeenCalled(); - const [ , storage ] = create.calls.mostRecent().args; + const storage = create.calls.mostRecent().args[ 1 ]; storage.mockBackends.session.test = '"mockValue"'; expect( storage.session.getItem( 'test' ) ).toEqual( 'mockValue' ); } ); @@ -117,13 +113,35 @@ describe( 'A laxar-mocks test runner', () => { ///////////////////////////////////////////////////////////////////////////////////////////////////// it( 'provides a mock-log injection without side-effects', () => { - const [ , , log ] = create.calls.mostRecent().args; + const log = create.calls.mostRecent().args[ 2 ]; log.error( 'test error' ); expect( log.error ).toHaveBeenCalled(); } ); ///////////////////////////////////////////////////////////////////////////////////////////////////// + it( 'provides a mock-configuration injection with the values given to setup', () => { + const configuration = create.calls.mostRecent().args[ 3 ]; + expect( configuration.get( 'baseHref' ) ).toEqual( '/' ); + expect( configuration.get ).toHaveBeenCalled(); + } ); + + ///////////////////////////////////////////////////////////////////////////////////////////////////// + + it( 'provides a mock-assets injection with the values given to setup', done => { + const assets = create.calls.mostRecent().args[ 4 ]; + + assets.forTheme( 'some-widget.html' ) + .then( html => { + expect( html ).toEqual( '

hey

' ); + } ) + .then( done, done.fail ); + + expect( assets.forTheme ).toHaveBeenCalled(); + } ); + + ///////////////////////////////////////////////////////////////////////////////////////////////////// + describe( 'and then to render the widget', () => { let container;