6.0.0 • Published 6 months ago

sham-ui-test-helpers v6.0.0

Weekly downloads
32
License
MIT
Repository
github
Last release
6 months ago

sham-ui-test-helpers

Install

yarn add sham-ui-test-helpers --dev

API

Table of Contents

renderer

Render component with options

Parameters
  • componentClass Class<Component> Component class for rendering
  • componentOptions Object Options (optional, default {})
  • context Object Extra root context parameters (optional, default {})
Examples
import Label from './Label.sht';
import renderer from 'sham-ui-test-helpers';

it( 'renders correctly', () => {
    const meta = renderer( Label );

    expect( meta.ctx.ID ).toEqual( 'component' );
    expect( meta.ctx.container.innerHTML ).toEqual( 'Foo' );
} );
import Label from './Label.sht';
import renderer from 'sham-ui-test-helpers';

it( 'snapshot correctly', () => {
    const meta = renderer( Label );

    expect( meta.toJSON() ).toMatchSnapshot()
} );

Returns RenderResult

Component

sham-ui component

DI

sham-ui di container

RenderResult

Result of renderer

Type: Object

Properties
  • component Component Rendered component instance
  • DI DI Container, used for render
  • ctx Object Context of rendered component
  • toJSON ToJSON Dump to JSON for jest's snapshot testing

ToJSON

Function for dump render result (using for jest-snapshots)

Type: Function

Returns RenderResultSnapshot

RenderResultSnapshot

Type: Object

Properties

compile

Compile component. Can call with mapping object

Parameters
Examples
import renderer, { compile } from 'sham-ui-test-helpers';
it( 'inline', () => {
  const meta = renderer(
      compile`
          <main>
              <div>
                  {{title}}
              </div>
              <div>
                  {{content}}
              </div>
          </main>

      `,
      {
          title: 'title from options',
          content: 'content from options'
      }
  );
  expect( meta.toJSON() ).toMatchSnapshot();
  expect( meta.component ).toBeInstanceOf( Component );
  meta.component.update( {
      title: 'new title',
      content: 'new content'
  } );
  expect( meta.toJSON() ).toMatchSnapshot();
} );
import renderer, { compile } from 'sham-ui-test-helpers';
it( 'inline with mappings', () => {
  const meta = renderer(
      compile( {
          TitleComponent: compile`<h1>{{text}}</h1>`
      } )`
          <TitleComponent text={{title}}/>

          <main>
              <div>
                  {{title}}
              </div>
              <div>
                  {{content}}
              </div>
          </main>

      `,
      {
          title: 'title from options',
          content: 'content from options'
      }
  );
  expect( meta.toJSON() ).toMatchSnapshot();
  expect( meta.component ).toBeInstanceOf( Component );
  meta.component.update( {
      title: 'new title',
      content: 'new content'
  } );
  expect( meta.toJSON() ).toMatchSnapshot();
} );

Returns (Component | Function)

compileAsSFC

Compile as single file component (SFС). Also can call with mapping object

Parameters
Examples
import renderer, { compileAsSFC } from 'sham-ui-test-helpers';
it( 'sfc', () => {
  const meta = renderer(
      compileAsSFC`
          <template>
              <div>
                  {{title}}
              </div>
              <div>
                  {{content}}
              </div>
          </template>

          <script>
              function dummy( options ) {
                  const title = ref();
                  const content = ref();
                  options( {
                      [ title ]: 'Default title',
                      [ content ]: 'Default content'
                  } )
              }
              export default( Template, dummy );
          </script>
      `,
      {
          title: 'title from options',
          content: 'content from options'
      }
  );
  expect( meta.toJSON() ).toMatchSnapshot();
  expect( meta.component ).toBeInstanceOf( Component );
  meta.component.update( {
      title: 'new title',
      content: 'new content'
  } );
  expect( meta.toJSON() ).toMatchSnapshot();
} );
import renderer, { compileAsSFC } from 'sham-ui-test-helpers';
it( 'sfc with mappings', () => {
  const meta = renderer(
      compileAsSFC( {
          Header: compile`<header>{{text}}</header>`
      } )`
          <template>
              <Header text={{title}}/>

              <div>
                  {{title}}
              </div>
              <div>
                  {{content}}
              </div>
          </template>

          <script>
              function dummy( options ) {
                  const title = ref();
                  const content = ref();
                  options( {
                      [ title ]: 'Default title',
                      [ content ]: 'Default content'
                  } )
              }
              export default( Template, dummy );
          </script>
      `,
      {
          title: 'title from sfc options',
          content: 'content from sfc options'
      }
  );
  expect( meta.toJSON() ).toMatchSnapshot();
  expect( meta.component ).toBeInstanceOf( Component );
  meta.component.update( {
      title: 'new sfc title',
      content: 'new sfc content'
  } );
  expect( meta.toJSON() ).toMatchSnapshot();
} );

Returns (Component | Function)

6.0.0-alpha.7

7 months ago

6.0.0

6 months ago

6.0.0-alpha.6

7 months ago

6.0.0-alpha.4

2 years ago

6.0.0-alpha.5

1 year ago

6.0.0-alpha.3

2 years ago

6.0.0-alpha.1

2 years ago

6.0.0-alpha.2

2 years ago

5.0.0

2 years ago

5.0.0-alpha.15

2 years ago

5.0.0-alpha.16

2 years ago

5.0.0-alpha.14

2 years ago

5.0.0-alpha.13

2 years ago

5.0.0-alpha.12

3 years ago

5.0.0-alpha.11

3 years ago

5.0.0-alpha.10

3 years ago

5.0.0-alpha.9

3 years ago

5.0.0-alpha.7

3 years ago

5.0.0-alpha.8

3 years ago

5.0.0-alpha.6

3 years ago

5.0.0-alpha.5

3 years ago

5.0.0-alpha.4

3 years ago

5.0.0-alpha.3

3 years ago

5.0.0-alpha.2

3 years ago

5.0.0-alpha.1

3 years ago

5.0.0-alpha.0

3 years ago

4.1.3

3 years ago

4.1.2

3 years ago

4.1.1

3 years ago

4.1.0

3 years ago

4.0.3

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

5 years ago

3.3.0

5 years ago

3.2.1

5 years ago

3.1.5

5 years ago

3.1.4

5 years ago

3.1.3

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

1.8.0

6 years ago

1.7.1

6 years ago

1.7.0

6 years ago

1.6.5

6 years ago

1.6.4

6 years ago

1.6.3

6 years ago

1.6.2

6 years ago

1.6.1

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago