Methods
(static) hasInstance(Component) → {boolean}
Checks if a component has been instantiated.
Parameters:
Name | Type | Description |
---|---|---|
Component |
Class.<Component> |
A mocked component constructor |
- Source:
Examples
<script>
import Component from './Component.svelte'
import OtherComponent from './OtherComponent.svelte'
</script>
<Component />
<OtherComponent />
jest.mock('Component.svelte');
jest.mock('OtherComponent.svelte');
jest.mock('NonExistentComponent.svelte');
import Component from 'Component.svelte';
import OtherComponent from 'OtherComponent.svelte';
import NonExistentComponent from 'NonExistentComponent.svelte';
svelteMock.mockImplementation(Component);
svelteMock.mockImplementation(OtherComponent);
svelteMock.mockImplementation(NonExistentComponent);
import App from 'App.svelte';
new App();
hasInstance(Component); // true
hasInstance(OtherComponent); // true
hasInstance(NonExistentComponent); // false
(static) hasInstanceMatching(Component, matchFn) → {boolean}
Checks if an instance of a component matches the matchFn
.
Parameters:
Name | Type | Description |
---|---|---|
Component |
Class.<Component> |
A mocked component constructor |
matchFn |
function |
A function that takes an instance of a component and returns true if a condition is matched |
Examples
<script>
import Component from './Component.svelte'
</script>
<Component first=firstValue second=secondValue />
<Component on:click="clickFn()" on:custom="customFn()" />
// Import mocked components
jest.mock('Component.svelte');
import Component from 'Component.svelte';
svelteMock.mockImplementation(Component);
// Import and render app
import App from 'App.svelte';
new App();
// true
hasInstanceMatching(
Component,
(component) => hasProps(component, ['first']),
);
// false
hasInstanceMatching(
Component,
(component) => hasProps(component, ['nonExistent']),
);
// true
hasInstanceMatching(
Component,
(component) => hasEventHandlers(component, ['click']),
);
// false
hasInstanceMatching(
Component,
(component) => hasEventHandlers(component, ['nonExistent']),
);
(static) hasInstanceWithBoundProps(Component, boundProps) → {boolean}
Checks if an instance of a component has the specified bound props.
Parameters:
Name | Type | Description |
---|---|---|
Component |
Class.<Component> |
A mocked component constructor |
boundProps |
Array | Object |
The bound props to check |
Examples
<script>
import Component from './Component.svelte'
</script>
<Component bind:first=firstValue bind:second=secondValue />
<Component bind:third=thirdValue />
// Import mocked components
jest.mock('Component.svelte');
import Component from 'Component.svelte';
svelteMock.mockImplementation(Component);
// Import and render app
import App from 'App.svelte';
new App();
hasInstanceWithBoundProps(Component, ['first']); // true
hasInstanceWithBoundProps(Component, ['first', 'second']); // true
hasInstanceWithBoundProps(Component, ['nonExistent'])); // false
hasInstanceWithBoundProps(Component, { third: thirdValue }); // true
hasInstanceWithBoundProps(Component, { third: wrongValue }); // false
(static) hasInstanceWithEventHandlers(Component, eventHandlers) → {boolean}
Checks if an instance of a component has the specified event handlers.
Parameters:
Name | Type | Description |
---|---|---|
Component |
Class.<Component> |
A mocked component constructor |
eventHandlers |
Array | Object |
The event handlers to check |
Examples
<script>
import Component from './Component.svelte'
</script>
<Component on:click="clickFn()" on:custom="customFn()" />
<Component on:focus="focusFn()" />
// Import mocked components
jest.mock('Component.svelte');
import Component from 'Component.svelte';
svelteMock.mockImplementation(Component);
// Import and render app
import App from 'App.svelte';
new App();
hasInstanceWithEventHandlers(Component, ['click']); // true
hasInstanceWithEventHandlers(Component, ['click', 'custom']); // true
hasInstanceWithEventHandlers(Component, ['nonExistent'])); // false
hasInstanceWithEventHandlers(Component, { focus: focusFn }); // true
hasInstanceWithEventHandlers(Component, { focus: wrongFn }); // false
(static) hasInstanceWithProps(Component, props) → {boolean}
Checks if an instance of a component has the specified props.
Parameters:
Name | Type | Description |
---|---|---|
Component |
Class.<Component> |
A mocked component constructor |
props |
Array | Object |
The props to check |
Examples
<script>
import Component from './Component.svelte'
</script>
<Component first={firstValue} second={secondValue} />
<Component third={thirdValue} />
// Import mocked components
jest.mock('Component.svelte');
import Component from 'Component.svelte';
svelteMock.mockImplementation(Component);
// Import and render app
import App from 'App.svelte';
new App();
hasInstanceWithProps(Component, ['first']); // true
hasInstanceWithProps(Component, ['first', 'second']); // true
hasInstanceWithProps(Component, ['nonExistent'])); // false
hasInstanceWithProps(Component, { third: thirdValue }); // true
hasInstanceWithProps(Component, { third: wrongValue }); // false
(static) hasInstanceWithSlots(Component, slotsopt) → {boolean}
Checks if an instance of a component has the specified slots.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
Component |
Class.<Component> |
A mocked component constructor |
|
slots |
Array | Object |
<optional> |
The slots to check. Default/unnamed slot is checked if this parameter is not provided. |
Examples
<script>
import Component from './Component.svelte'
</script>
<Component>
<span>First</span>
</Component>
<Component>
<span slot="first">First</span>
<span slot="second">Second</span>
</Component>
// Import mocked components
jest.mock('Component.svelte');
import Component from 'Component.svelte';
svelteMock.mockImplementation(Component);
// Import and render app
import App from 'App.svelte';
new App();
// Check for unnamed slot
hasInstanceWithSlots(Component); // true
// Check for named slots
hasInstanceWithSlots(Component, ['first']); // true
hasInstanceWithSlots(Component, ['first', 'second']); // true
hasInstanceWithSlots(Component, ['nonExistent'])); // false
hasInstanceWithSlots(Component, { first: firstSlot }); // true
hasInstanceWithSlots(Component, { first: wrongSlot }); // false
(static) isComponent(object) → {boolean}
Checks if an object is a component constructor.
Parameters:
Name | Type | Description |
---|---|---|
object |
Object |
An object to check if it is a component or not |
- Source: