expect/class

Methods

(static) toHaveInstanceWithBoundProps(Component, boundProps)

Passes if a mocked component class has an instance with bound props.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

boundProps Array | Object

The bound props to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component bind:first=firstValue bind:second=secondValue />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithBoundProps(['first']);
expect(Component).toHaveInstanceWithBoundProps(['first', 'second']);
expect(Component).not.toHaveInstanceWithBoundProps(['nonExistent']);
expect(Component).toHaveInstanceWithBoundProps({ first: firstValue });
expect(Component).not.toHaveInstanceWithBoundProps({ first: wrongValue });

(static) toHaveInstanceWithEventHandlers(Component, eventHandlers)

Passes if a mocked component class has an instance with event handlers.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

eventHandlers Array | Object

The event handlers to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component on:click="clickFn()" on:custom="customFn()" />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithEventHandlers(['click']);
expect(Component).toHaveInstanceWithEventHandlers(['click', 'custom']);
expect(Component).not.toHaveInstanceWithEventHandlers(['nonExistent']);
expect(Component).toHaveInstanceWithEventHandlers({ click: clickFn });
expect(Component).not.toHaveInstanceWithEventHandlers({ click: wrongFn });

(static) toHaveInstanceWithProps(Component, props)

Passes if a mocked component class has an instance with props.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

props Array | Object

The props to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component first={firstValue} second={secondValue} />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithProps(['first']);
expect(Component).toHaveInstanceWithProps(['first', 'second']);
expect(Component).not.toHaveInstanceWithProps(['nonExistent']);
expect(Component).toHaveInstanceWithProps({ first: firstValue });
expect(Component).not.toHaveInstanceWithProps({ first: wrongValue });

(static) toHaveInstanceWithSlots(Component, slotsopt)

Passes if a mocked component class has an instance with 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.

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component>
  <span>First</span>
</Component>
<Component>
  <span slot="first">First</span>
  <span slot="second">Second</span>
</Component>

Test code (App.spec.js)

// 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
expect(Component).toHaveInstanceWithSlots();
// Check for named slots
expect(Component).toHaveInstanceWithSlots(['first']);
expect(Component).toHaveInstanceWithSlots(['first', 'second']);
expect(Component).not.toHaveInstanceWithSlots(['nonExistent']);
expect(Component).toHaveInstanceWithSlots({ first: firstSlot });
expect(Component).not.toHaveInstanceWithSlots({ first: wrongSlot });

Methods

(static) toHaveInstanceWithBoundProps(Component, boundProps)

Passes if a mocked component class has an instance with bound props.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

boundProps Array | Object

The bound props to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component bind:first=firstValue bind:second=secondValue />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithBoundProps(['first']);
expect(Component).toHaveInstanceWithBoundProps(['first', 'second']);
expect(Component).not.toHaveInstanceWithBoundProps(['nonExistent']);
expect(Component).toHaveInstanceWithBoundProps({ first: firstValue });
expect(Component).not.toHaveInstanceWithBoundProps({ first: wrongValue });

(static) toHaveInstanceWithEventHandlers(Component, eventHandlers)

Passes if a mocked component class has an instance with event handlers.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

eventHandlers Array | Object

The event handlers to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component on:click="clickFn()" on:custom="customFn()" />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithEventHandlers(['click']);
expect(Component).toHaveInstanceWithEventHandlers(['click', 'custom']);
expect(Component).not.toHaveInstanceWithEventHandlers(['nonExistent']);
expect(Component).toHaveInstanceWithEventHandlers({ click: clickFn });
expect(Component).not.toHaveInstanceWithEventHandlers({ click: wrongFn });

(static) toHaveInstanceWithProps(Component, props)

Passes if a mocked component class has an instance with props.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

props Array | Object

The props to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component first={firstValue} second={secondValue} />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithProps(['first']);
expect(Component).toHaveInstanceWithProps(['first', 'second']);
expect(Component).not.toHaveInstanceWithProps(['nonExistent']);
expect(Component).toHaveInstanceWithProps({ first: firstValue });
expect(Component).not.toHaveInstanceWithProps({ first: wrongValue });

(static) toHaveInstanceWithSlots(Component, slotsopt)

Passes if a mocked component class has an instance with 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.

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component>
  <span>First</span>
</Component>
<Component>
  <span slot="first">First</span>
  <span slot="second">Second</span>
</Component>

Test code (App.spec.js)

// 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
expect(Component).toHaveInstanceWithSlots();
// Check for named slots
expect(Component).toHaveInstanceWithSlots(['first']);
expect(Component).toHaveInstanceWithSlots(['first', 'second']);
expect(Component).not.toHaveInstanceWithSlots(['nonExistent']);
expect(Component).toHaveInstanceWithSlots({ first: firstSlot });
expect(Component).not.toHaveInstanceWithSlots({ first: wrongSlot });

Methods

(static) toHaveInstanceWithBoundProps(Component, boundProps)

Passes if a mocked component class has an instance with bound props.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

boundProps Array | Object

The bound props to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component bind:first=firstValue bind:second=secondValue />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithBoundProps(['first']);
expect(Component).toHaveInstanceWithBoundProps(['first', 'second']);
expect(Component).not.toHaveInstanceWithBoundProps(['nonExistent']);
expect(Component).toHaveInstanceWithBoundProps({ first: firstValue });
expect(Component).not.toHaveInstanceWithBoundProps({ first: wrongValue });

(static) toHaveInstanceWithEventHandlers(Component, eventHandlers)

Passes if a mocked component class has an instance with event handlers.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

eventHandlers Array | Object

The event handlers to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component on:click="clickFn()" on:custom="customFn()" />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithEventHandlers(['click']);
expect(Component).toHaveInstanceWithEventHandlers(['click', 'custom']);
expect(Component).not.toHaveInstanceWithEventHandlers(['nonExistent']);
expect(Component).toHaveInstanceWithEventHandlers({ click: clickFn });
expect(Component).not.toHaveInstanceWithEventHandlers({ click: wrongFn });

(static) toHaveInstanceWithProps(Component, props)

Passes if a mocked component class has an instance with props.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

props Array | Object

The props to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component first={firstValue} second={secondValue} />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithProps(['first']);
expect(Component).toHaveInstanceWithProps(['first', 'second']);
expect(Component).not.toHaveInstanceWithProps(['nonExistent']);
expect(Component).toHaveInstanceWithProps({ first: firstValue });
expect(Component).not.toHaveInstanceWithProps({ first: wrongValue });

(static) toHaveInstanceWithSlots(Component, slotsopt)

Passes if a mocked component class has an instance with 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.

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component>
  <span>First</span>
</Component>
<Component>
  <span slot="first">First</span>
  <span slot="second">Second</span>
</Component>

Test code (App.spec.js)

// 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
expect(Component).toHaveInstanceWithSlots();
// Check for named slots
expect(Component).toHaveInstanceWithSlots(['first']);
expect(Component).toHaveInstanceWithSlots(['first', 'second']);
expect(Component).not.toHaveInstanceWithSlots(['nonExistent']);
expect(Component).toHaveInstanceWithSlots({ first: firstSlot });
expect(Component).not.toHaveInstanceWithSlots({ first: wrongSlot });

Methods

(static) toHaveInstanceWithBoundProps(Component, boundProps)

Passes if a mocked component class has an instance with bound props.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

boundProps Array | Object

The bound props to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component bind:first=firstValue bind:second=secondValue />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithBoundProps(['first']);
expect(Component).toHaveInstanceWithBoundProps(['first', 'second']);
expect(Component).not.toHaveInstanceWithBoundProps(['nonExistent']);
expect(Component).toHaveInstanceWithBoundProps({ first: firstValue });
expect(Component).not.toHaveInstanceWithBoundProps({ first: wrongValue });

(static) toHaveInstanceWithEventHandlers(Component, eventHandlers)

Passes if a mocked component class has an instance with event handlers.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

eventHandlers Array | Object

The event handlers to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component on:click="clickFn()" on:custom="customFn()" />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithEventHandlers(['click']);
expect(Component).toHaveInstanceWithEventHandlers(['click', 'custom']);
expect(Component).not.toHaveInstanceWithEventHandlers(['nonExistent']);
expect(Component).toHaveInstanceWithEventHandlers({ click: clickFn });
expect(Component).not.toHaveInstanceWithEventHandlers({ click: wrongFn });

(static) toHaveInstanceWithProps(Component, props)

Passes if a mocked component class has an instance with props.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

props Array | Object

The props to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component first={firstValue} second={secondValue} />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithProps(['first']);
expect(Component).toHaveInstanceWithProps(['first', 'second']);
expect(Component).not.toHaveInstanceWithProps(['nonExistent']);
expect(Component).toHaveInstanceWithProps({ first: firstValue });
expect(Component).not.toHaveInstanceWithProps({ first: wrongValue });

(static) toHaveInstanceWithSlots(Component, slotsopt)

Passes if a mocked component class has an instance with 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.

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component>
  <span>First</span>
</Component>
<Component>
  <span slot="first">First</span>
  <span slot="second">Second</span>
</Component>

Test code (App.spec.js)

// 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
expect(Component).toHaveInstanceWithSlots();
// Check for named slots
expect(Component).toHaveInstanceWithSlots(['first']);
expect(Component).toHaveInstanceWithSlots(['first', 'second']);
expect(Component).not.toHaveInstanceWithSlots(['nonExistent']);
expect(Component).toHaveInstanceWithSlots({ first: firstSlot });
expect(Component).not.toHaveInstanceWithSlots({ first: wrongSlot });

Methods

(static) toHaveInstanceWithBoundProps(Component, boundProps)

Passes if a mocked component class has an instance with bound props.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

boundProps Array | Object

The bound props to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component bind:first=firstValue bind:second=secondValue />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithBoundProps(['first']);
expect(Component).toHaveInstanceWithBoundProps(['first', 'second']);
expect(Component).not.toHaveInstanceWithBoundProps(['nonExistent']);
expect(Component).toHaveInstanceWithBoundProps({ first: firstValue });
expect(Component).not.toHaveInstanceWithBoundProps({ first: wrongValue });

(static) toHaveInstanceWithEventHandlers(Component, eventHandlers)

Passes if a mocked component class has an instance with event handlers.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

eventHandlers Array | Object

The event handlers to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component on:click="clickFn()" on:custom="customFn()" />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithEventHandlers(['click']);
expect(Component).toHaveInstanceWithEventHandlers(['click', 'custom']);
expect(Component).not.toHaveInstanceWithEventHandlers(['nonExistent']);
expect(Component).toHaveInstanceWithEventHandlers({ click: clickFn });
expect(Component).not.toHaveInstanceWithEventHandlers({ click: wrongFn });

(static) toHaveInstanceWithProps(Component, props)

Passes if a mocked component class has an instance with props.

Parameters:
Name Type Description
Component Class.<Component>

A mocked component constructor

props Array | Object

The props to check

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component first={firstValue} second={secondValue} />

Test code (App.spec.js)

// 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();

expect(Component).toHaveInstanceWithProps(['first']);
expect(Component).toHaveInstanceWithProps(['first', 'second']);
expect(Component).not.toHaveInstanceWithProps(['nonExistent']);
expect(Component).toHaveInstanceWithProps({ first: firstValue });
expect(Component).not.toHaveInstanceWithProps({ first: wrongValue });

(static) toHaveInstanceWithSlots(Component, slotsopt)

Passes if a mocked component class has an instance with 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.

Source:
Examples

Svelte code (App.svelte)

<script>
   import Component from './Component.svelte'
</script>

<Component>
  <span>First</span>
</Component>
<Component>
  <span slot="first">First</span>
  <span slot="second">Second</span>
</Component>

Test code (App.spec.js)

// 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
expect(Component).toHaveInstanceWithSlots();
// Check for named slots
expect(Component).toHaveInstanceWithSlots(['first']);
expect(Component).toHaveInstanceWithSlots(['first', 'second']);
expect(Component).not.toHaveInstanceWithSlots(['nonExistent']);
expect(Component).toHaveInstanceWithSlots({ first: firstSlot });
expect(Component).not.toHaveInstanceWithSlots({ first: wrongSlot });