@instawork/testing
Advanced tools
Comparing version 1.0.0-pre.1 to 1.0.0-pre.3
@@ -1,18 +0,1 @@ | ||
/// <reference types="jquery" /> | ||
import type { DomHarnessTestContext } from '../../'; | ||
export interface DomHarnessTestContextInternal<THost extends HTMLElement = HTMLElement, TContext extends DomHarnessTestContext<THost> = DomHarnessTestContext<THost>> extends DomHarnessTestContext<THost> { | ||
readonly vars: TContext; | ||
tearDown(): void; | ||
} | ||
/** | ||
* Holds references to per-test objects, includes logic for cleaning them up. | ||
* | ||
* @internal | ||
*/ | ||
export declare class DomHarnessTestContextImpl<THost extends HTMLElement = HTMLElement> implements DomHarnessTestContextInternal<THost> { | ||
readonly $fragment: JQuery; | ||
readonly $host: JQuery<THost>; | ||
get vars(): DomHarnessTestContext<THost>; | ||
constructor($fragment: JQuery, $host: JQuery<THost>); | ||
tearDown(): void; | ||
} | ||
export {}; |
@@ -7,7 +7,6 @@ /** | ||
export class DomHarnessTestContextImpl { | ||
constructor($fragment, $host) { | ||
constructor($fragment, $host, errors) { | ||
this.$fragment = $fragment; | ||
this.$host = $host; | ||
this.$fragment = $fragment; | ||
this.$host = $host; | ||
this.errors = errors; | ||
} | ||
@@ -18,2 +17,3 @@ get vars() { | ||
$host: this.$host, | ||
errors: this.errors, | ||
}; | ||
@@ -20,0 +20,0 @@ } |
{ | ||
"name": "@instawork/testing", | ||
"version": "1.0.0-pre.1", | ||
"version": "1.0.0-pre.3", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"peerDependencies": { | ||
"chai": "^4.3.4", | ||
"mocha": "^8.3.1" | ||
}, | ||
"scripts": { | ||
"postpublish": "../../scripts/dist_tag" | ||
} | ||
} |
@@ -5,2 +5,3 @@ /// <reference types="jquery" /> | ||
readonly $host: JQuery<THost>; | ||
readonly errors: Error[]; | ||
} |
@@ -20,9 +20,11 @@ /// <reference types="jquery" /> | ||
attrs?: { | ||
[attrName: string]: string | number; | ||
[attrName: string]: string | number | true; | ||
}; | ||
/** | ||
* When set to `true, skip automatically checking for errors during setUp | ||
*/ | ||
skipErrorCheck?: boolean; | ||
} | ||
/** | ||
* A unit testing utility class that handles setting up and tearing down DOM fragments for testing DOM-based features. | ||
* | ||
* @internal | ||
*/ | ||
@@ -38,2 +40,3 @@ export declare class DomHarness<THost extends HTMLElement = HTMLElement, TContext extends DomHarnessTestContext<THost> = DomHarnessTestContext<THost>> { | ||
get $host(): JQuery<THost>; | ||
get errors(): Error[]; | ||
/** | ||
@@ -50,4 +53,2 @@ * @param template - HTML content that will be used to create the DOM fragment that hosts the feature being tested | ||
* Creates and initializes the DOM fragment, returns a {@link DomHarnessTestContext} instance. | ||
* | ||
* @param {DomHarnessSetUpOptions} options | ||
*/ | ||
@@ -78,3 +79,3 @@ setUp(options?: DomHarnessSetUpOptions<THost, TContext>): void; | ||
requireFocus(): void; | ||
protected createContext($fragment: JQuery, $host: JQuery<THost>): TContext & DomHarnessTestContextInternal<THost, TContext>; | ||
protected createContext($fragment: JQuery, $host: JQuery<THost>, errors: Error[]): TContext & DomHarnessTestContextInternal<THost, TContext>; | ||
} | ||
@@ -90,4 +91,3 @@ /** | ||
* @param domTarget - A CSS / jQuery selector that will be used to add the DOM fragment to the test page DOM | ||
* @returns {DomHarness} | ||
*/ | ||
export declare function domHarness<THost extends HTMLElement = HTMLElement>(template: string, pluginFnName?: string | string[], selector?: string, domTarget?: string): DomHarness<THost>; |
import { DomHarnessTestContextImpl } from '@instawork/testing/internal'; | ||
import * as $ from 'jquery'; | ||
/** | ||
* A unit testing utility class that handles setting up and tearing down DOM fragments for testing DOM-based features. | ||
* | ||
* @internal | ||
*/ | ||
@@ -35,9 +34,11 @@ export class DomHarness { | ||
} | ||
get errors() { | ||
return this.context?.errors; | ||
} | ||
/** | ||
* Creates and initializes the DOM fragment, returns a {@link DomHarnessTestContext} instance. | ||
* | ||
* @param {DomHarnessSetUpOptions} options | ||
*/ | ||
setUp(options = {}) { | ||
const $fragment = $(this.template.replace(/\n/, '')); | ||
const errors = []; | ||
if (typeof options.domInit === 'function') { | ||
@@ -47,3 +48,5 @@ options.domInit($fragment); | ||
if (options.attrs) { | ||
Object.entries(options.attrs).forEach(([attr, value]) => $fragment.attr(attr, value)); | ||
Object.entries(options.attrs).forEach(([attr, value]) => { | ||
$fragment.attr(attr, value === true ? '' : value); | ||
}); | ||
} | ||
@@ -57,3 +60,8 @@ $fragment.appendTo(this.domTarget); | ||
} | ||
pluginHost[pluginFnName](); | ||
try { | ||
pluginHost[pluginFnName](); | ||
} | ||
catch (err) { | ||
errors.push(err); | ||
} | ||
}); | ||
@@ -67,6 +75,9 @@ if (this._context) { | ||
: $fragment); | ||
this._context = this.createContext($fragment, $host); | ||
this._context = this.createContext($fragment, $host, errors); | ||
if (typeof options.contextInit === 'function') { | ||
options.contextInit(this._context.vars); | ||
} | ||
if (!options.skipErrorCheck) { | ||
expect(errors, errors[0]?.stack).to.be.empty; | ||
} | ||
} | ||
@@ -109,4 +120,4 @@ /** | ||
} | ||
createContext($fragment, $host) { | ||
return new DomHarnessTestContextImpl($fragment, $host); | ||
createContext($fragment, $host, errors) { | ||
return new DomHarnessTestContextImpl($fragment, $host, errors); | ||
} | ||
@@ -123,3 +134,2 @@ } | ||
* @param domTarget - A CSS / jQuery selector that will be used to add the DOM fragment to the test page DOM | ||
* @returns {DomHarness} | ||
*/ | ||
@@ -126,0 +136,0 @@ export function domHarness(template, pluginFnName = [], selector = undefined, domTarget = 'body') { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
17713
17
2
267