@limetech/lime-web-components-testing
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -6,2 +6,14 @@ # Change Log | ||
# [3.1.0](https://github.com/Lundalogik/lime-web-components/compare/v3.0.0...v3.1.0) (2020-05-04) | ||
### Features | ||
* add functions for setting the state ([d1d632e](https://github.com/Lundalogik/lime-web-components/commit/d1d632eba8a188a4e201f025b2173410c25974e4)) | ||
* add test fixtures for limeobjects ([7389723](https://github.com/Lundalogik/lime-web-components/commit/7389723dcce5420ba20f0a8c584c1e8da35fdde3)) | ||
# [3.0.0](https://github.com/Lundalogik/lime-web-components/compare/v2.6.2...v3.0.0) (2020-04-23) | ||
@@ -8,0 +20,0 @@ |
@@ -8,1 +8,3 @@ "use strict"; | ||
__export(require("./platform")); | ||
var limetypes_1 = require("./limetypes"); | ||
exports.limetypes = limetypes_1.limetypes; |
@@ -25,3 +25,2 @@ "use strict"; | ||
exports.createPlatform = createPlatform; | ||
exports.defaultPlatform = createDefaultPlatform(); | ||
exports.defaultContext = { | ||
@@ -48,7 +47,4 @@ limetype: null, | ||
} | ||
function createDefaultPlatform() { | ||
return createPlatform(); | ||
} | ||
function createDefaultService() { | ||
return createServiceProxy({}); | ||
return {}; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var core_1 = require("@stencil/core"); | ||
var platform_1 = require("./platform"); | ||
var DESTROY_FUNCTION_NAME = '__removeListeners'; | ||
function createComponent(page, name, config) { | ||
if (config === void 0) { config = {}; } | ||
var element = page.doc.createElement(name); | ||
var componentConfigs = new WeakMap(); | ||
var componentInstances = new WeakMap(); | ||
var originalHooks = new WeakMap(); | ||
var destroyFunctions = new WeakMap(); | ||
function createComponent(component, page, config) { | ||
componentConfigs.set(component.prototype, config); | ||
extendLifecycleHook(component.prototype, 'componentWillLoad', initComponent); | ||
var element = page.doc.createElement(config.tag); | ||
var props = config.props || {}; | ||
@@ -15,5 +20,5 @@ var listeners = config.listeners || {}; | ||
addChildren(element, children); | ||
element[DESTROY_FUNCTION_NAME] = function () { | ||
destroyFunctions.set(element, function () { | ||
removeListeners(listeners, element); | ||
}; | ||
}); | ||
page.body.appendChild(element); | ||
@@ -27,11 +32,44 @@ return element; | ||
} | ||
if (!(DESTROY_FUNCTION_NAME in component)) { | ||
var destroyFunction = destroyFunctions.get(component); | ||
if (!destroyFunction) { | ||
return; | ||
} | ||
component[DESTROY_FUNCTION_NAME](); | ||
destroyFunction(); | ||
} | ||
exports.destroyComponent = destroyComponent; | ||
function setState(component, state) { | ||
var instance = componentInstances.get(component); | ||
Object.assign(instance, state); | ||
} | ||
exports.setState = setState; | ||
function initComponent() { | ||
var element = core_1.getElement(this); | ||
componentInstances.set(element, this); | ||
var config = componentConfigs.get(this.constructor.prototype); | ||
if (config === null || config === void 0 ? void 0 : config.state) { | ||
setState(element, config.state); | ||
} | ||
} | ||
function extendLifecycleHook(component, name, hook) { | ||
if (!originalHooks.has(component)) { | ||
originalHooks.set(component, {}); | ||
} | ||
var hooks = originalHooks.get(component); | ||
if (!hooks[name]) { | ||
hooks[name] = component[name]; | ||
} | ||
component[name] = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
hook.apply(this, args); | ||
if (hooks[name]) { | ||
return hooks[name].apply(this, args); | ||
} | ||
}; | ||
} | ||
function setDefault(props) { | ||
if (!('platform' in props)) { | ||
props.platform = platform_1.defaultPlatform; | ||
props.platform = platform_1.createPlatform(); | ||
} | ||
@@ -38,0 +76,0 @@ if (!('context' in props)) { |
export * from './spec'; | ||
export * from './platform'; | ||
export { limetypes } from './limetypes'; |
export * from './spec'; | ||
export * from './platform'; | ||
export { limetypes } from './limetypes'; |
@@ -8,7 +8,2 @@ import { LimeWebComponentContext, LimeWebComponentPlatform } from '@limetech/lime-web-components-interfaces'; | ||
export declare function createPlatform(): LimeWebComponentPlatform; | ||
/** | ||
* A default platform that will be used if no platform is given | ||
* when a component is created | ||
*/ | ||
export declare const defaultPlatform: LimeWebComponentPlatform; | ||
export declare const defaultContext: LimeWebComponentContext; |
@@ -29,7 +29,2 @@ /** | ||
} | ||
/** | ||
* A default platform that will be used if no platform is given | ||
* when a component is created | ||
*/ | ||
export const defaultPlatform = createDefaultPlatform(); | ||
export const defaultContext = { | ||
@@ -62,7 +57,4 @@ limetype: null, | ||
} | ||
function createDefaultPlatform() { | ||
return createPlatform(); | ||
} | ||
function createDefaultService() { | ||
return createServiceProxy({}); | ||
return {}; | ||
} |
@@ -10,6 +10,14 @@ import { SpecPage } from '@stencil/core/testing'; | ||
/** | ||
* The props to give the component | ||
* Tag name of the component | ||
*/ | ||
tag: string; | ||
/** | ||
* The props to give the component when it is created | ||
*/ | ||
props?: ComponentProps; | ||
/** | ||
* The initial state to give the component when it is created | ||
*/ | ||
state?: object; | ||
/** | ||
* Any listeners to attach to the component | ||
@@ -26,4 +34,4 @@ */ | ||
* | ||
* @param {*} component the component class | ||
* @param {SpecPage} page the page to create the component on | ||
* @param {string} name the name of the component | ||
* @param {ComponentConfig} config component configuration | ||
@@ -33,3 +41,3 @@ * | ||
*/ | ||
export declare function createComponent(page: SpecPage, name: string, config?: ComponentConfig): HTMLElement; | ||
export declare function createComponent(component: any, page: SpecPage, config: ComponentConfig): HTMLElement; | ||
/** | ||
@@ -43,1 +51,10 @@ * Destroy a component that was created by the `createComponent` function | ||
export declare function destroyComponent(component: HTMLElement): void; | ||
/** | ||
* Set a state variable on the component | ||
* | ||
* @param {HTMLElement} component the component | ||
* @param {object} state the state variables to set along with their values | ||
* | ||
* @returns {void} | ||
*/ | ||
export declare function setState(component: HTMLElement, state: object): void; |
@@ -1,8 +0,12 @@ | ||
import { defaultContext, defaultPlatform } from './platform'; | ||
const DESTROY_FUNCTION_NAME = '__removeListeners'; | ||
import { getElement } from '@stencil/core'; | ||
import { defaultContext, createPlatform } from './platform'; | ||
const componentConfigs = new WeakMap(); | ||
const componentInstances = new WeakMap(); | ||
const originalHooks = new WeakMap(); | ||
const destroyFunctions = new WeakMap(); | ||
/** | ||
* Create a new component | ||
* | ||
* @param {*} component the component class | ||
* @param {SpecPage} page the page to create the component on | ||
* @param {string} name the name of the component | ||
* @param {ComponentConfig} config component configuration | ||
@@ -12,4 +16,6 @@ * | ||
*/ | ||
export function createComponent(page, name, config = {}) { | ||
const element = page.doc.createElement(name); | ||
export function createComponent(component, page, config) { | ||
componentConfigs.set(component.prototype, config); | ||
extendLifecycleHook(component.prototype, 'componentWillLoad', initComponent); | ||
const element = page.doc.createElement(config.tag); | ||
const props = config.props || {}; | ||
@@ -22,5 +28,5 @@ const listeners = config.listeners || {}; | ||
addChildren(element, children); | ||
element[DESTROY_FUNCTION_NAME] = () => { | ||
destroyFunctions.set(element, () => { | ||
removeListeners(listeners, element); | ||
}; | ||
}); | ||
page.body.appendChild(element); | ||
@@ -40,8 +46,62 @@ return element; | ||
} | ||
if (!(DESTROY_FUNCTION_NAME in component)) { | ||
const destroyFunction = destroyFunctions.get(component); | ||
if (!destroyFunction) { | ||
return; | ||
} | ||
component[DESTROY_FUNCTION_NAME](); | ||
destroyFunction(); | ||
} | ||
/** | ||
* Set a state variable on the component | ||
* | ||
* @param {HTMLElement} component the component | ||
* @param {object} state the state variables to set along with their values | ||
* | ||
* @returns {void} | ||
*/ | ||
export function setState(component, state) { | ||
const instance = componentInstances.get(component); | ||
Object.assign(instance, state); | ||
} | ||
/** | ||
* Init a component that is under test | ||
* | ||
* This is called just before the `componentWillLoad` lifecycle hook | ||
* of the component and is a good place to save the component instance | ||
* and set the initial state | ||
* | ||
* @returns {void} | ||
*/ | ||
function initComponent() { | ||
const element = getElement(this); | ||
componentInstances.set(element, this); | ||
const config = componentConfigs.get(this.constructor.prototype); | ||
if (config === null || config === void 0 ? void 0 : config.state) { | ||
setState(element, config.state); | ||
} | ||
} | ||
/** | ||
* Extend a lifecycle hook on a component | ||
* | ||
* @param {*} component the prototype of the component | ||
* @param {string} name the name of the hook | ||
* @param {Function} hook the function to run before the original hook | ||
* | ||
* @returns {void} | ||
*/ | ||
function extendLifecycleHook(component, name, hook) { | ||
if (!originalHooks.has(component)) { | ||
originalHooks.set(component, {}); | ||
} | ||
const hooks = originalHooks.get(component); | ||
if (!hooks[name]) { | ||
hooks[name] = component[name]; | ||
} | ||
component[name] = function (...args) { | ||
hook.apply(this, args); | ||
if (hooks[name]) { | ||
return hooks[name].apply(this, args); | ||
} | ||
}; | ||
} | ||
/** | ||
* Sets a default props on the component | ||
@@ -57,3 +117,3 @@ * | ||
if (!('platform' in props)) { | ||
props.platform = defaultPlatform; | ||
props.platform = createPlatform(); | ||
} | ||
@@ -60,0 +120,0 @@ if (!('context' in props)) { |
{ | ||
"name": "@limetech/lime-web-components-testing", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"author": "Lime Technologies", | ||
@@ -36,3 +36,3 @@ "homepage": "https://github.com/Lundalogik/lime-web-components", | ||
}, | ||
"gitHead": "c93988fca090e92d67e38b03247d6305c6224c7d" | ||
"gitHead": "a2e169ab7922e5db70a70859d6b63e16c863f22a" | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
743578
94
26435
1