How to use @open-wc/testing-helpers - 10 common examples

To help you get started, we’ve selected a few @open-wc/testing-helpers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github corpusculejs / custom-builtin-elements / __tests__ / index.js View on Github external
it('upgrades elements inserted via importNode to detached element', async () => {
      const name = generateName();

      const iframe = await fixture('');
      const foreignDocument = iframe.contentWindow.document;
      foreignDocument.body.innerHTML = `<div><a is=""></a></div>`;

      // eslint-disable-next-line prefer-destructuring
      const nodeToImport = foreignDocument.body.children[0];

      class Foo extends HTMLAnchorElement {}
      defineCBE(Foo, 'a', name);

      const wrapper = document.importNode(nodeToImport, true);
      expect(wrapper.children[0] instanceof Foo).toBeTruthy();
    });
  });
github corpusculejs / corpuscule / packages / element / __tests__ / query.ts View on Github external
this.attachShadow({mode: 'open'});
        }

        public connectedCallback(): void {
          // tslint:disable-next-line:no-inner-html
          this.shadowRoot!.innerHTML = `
            <div></div>
            <div class="wrapper">
              <div>Test text</div>
            </div>
          `;
        }
      }

      const tag = defineCE(Test);
      const test = await fixture(`&lt;${tag}&gt;`);

      expect(test.target).toEqual(jasmine.any(HTMLElement));
      expect(test.target.textContent).toBe('Test text');
    });
github corpusculejs / corpuscule / packages / element / __tests__ / element.ts View on Github external
public [propertyChangedCallback](...args: unknown[]): void {
          propertyChangedCallbackSpy(...args);
        }

        public [internalChangedCallback](...args: unknown[]): void {
          internalChangedCallbackSpy(...args);
        }

        protected [render](): null {
          return null;
        }
      }

      await customElements.whenDefined(tag);

      const test = fixtureSync(`&lt;${tag}&gt;`);

      test.attributeChangedCallback('attr', 'old', 'new');
      test[propertyChangedCallback]('attr', 'old', 'new');
      test[internalChangedCallback]('attr', 'old', 'new');

      await test.updateComplete;

      expect(attributeChangedCallbackSpy).not.toHaveBeenCalled();
      expect(propertyChangedCallbackSpy).not.toHaveBeenCalled();
      expect(internalChangedCallbackSpy).not.toHaveBeenCalled();
      // only during the connection
      expect(schedulerSpy).toHaveBeenCalledTimes(1);
    });
github corpusculejs / custom-builtin-elements / __tests__ / index.js View on Github external
class Bar extends HTMLAnchorElement {
        disconnectedCallback() {
          disconnectedCallbackSpy();
        }
      }
      const name = defineCBE(Bar, 'a');

      class Foo extends HTMLElement {
        constructor() {
          super();
          const root = this.attachShadow({mode: 'open'});
          root.innerHTML = `<a is=""></a>`;
        }
      }

      const tag = defineCE(Foo);
      const foo = await fixture(`&lt;${tag}&gt;`);

      // eslint-disable-next-line prefer-destructuring
      const bar = foo.shadowRoot.children[0];

      const promise = waitForMutationObserverChange(
        foo.shadowRoot,
        observeChildren,
      );
      bar.parentNode.removeChild(bar);

      await promise;

      expect(disconnectedCallbackSpy).toHaveBeenCalledTimes(1);
    });
github open-wc / open-wc / packages / testing / register-fixture-cleanup.js View on Github external
afterEach(() => {
    if (cachedWrappers) {
      cachedWrappers.forEach(wrapper => {
        document.body.removeChild(wrapper);
      });
    }
    cachedWrappers.length = 0; // reset it like this as we can't reassign it
  });
}
github open-wc / open-wc / packages / testing / register-fixture-cleanup.js View on Github external
afterEach(() => {
    if (cachedWrappers) {
      cachedWrappers.forEach(wrapper => {
        document.body.removeChild(wrapper);
      });
    }
    cachedWrappers.length = 0; // reset it like this as we can't reassign it
  });
}
github corpusculejs / corpuscule / packages / context / __tests__ / index.ts View on Github external
it('throws an error if no provider exists for context', done =&gt; {
    @consumer
    class Consumer extends CustomElement {
      @value public contextValue!: number;
    }

    const tag = defineCE(Consumer);

    window.onerror = message =&gt; {
      expect(message).toContain('No provider found for Consumer');
      done();
    };

    fixture(`&lt;${tag}&gt;`);
  });
github corpusculejs / corpuscule / packages / element / __tests__ / attribute.ts View on Github external
it('initializes and fills "observedAttributes"', () =&gt; {
      class Test extends CustomElement {
        public static readonly observedAttributes: ReadonlyArray = [];

        @attribute('a1', Boolean)
        public attr1: boolean | null = null;

        @attribute('a2', String)
        public attr2: string | null = null;
      }

      defineCE(Test);
      expect(Test.observedAttributes).toEqual(['a1', 'a2']);
    });
github corpusculejs / corpuscule / packages / form / __tests__ / field.ts View on Github external
@gear() public readonly state!: FormState;

          @option()
          public onSubmit(): void {}
        }

        @field()
        class Field extends CustomElement {
          @gear() public readonly formApi!: FormApi;
          @gear() public readonly input!: FieldInputProps;
          @gear() public readonly meta!: FieldMetaProps;

          @option() public readonly name: string = 'test';
        }

        const fieldTag = defineCE(Field);
        const formTag = defineCE(Form);

        const formElement = await fixture(`
          &lt;${formTag}&gt;
            &lt;${fieldTag}&gt;
              <input type="text">
            
          
        `);

        const inputElement = formElement.querySelector('input')!;

        inputElement.dispatchEvent(new Event('focusin', {bubbles: true}));

        expect(state.focus).toHaveBeenCalled();
      });
github corpusculejs / corpuscule / packages / context / __tests__ / index.ts View on Github external
it('provides context for all consumers', async () =&gt; {
    @provider
    class Provider extends CustomElement {
      @value public providingValue: number = 2;
    }

    @consumer
    class Consumer extends CustomElement {
      @value public contextValue!: number;
    }

    const providerTag = defineCE(Provider);
    const consumerTag = defineCE(Consumer);

    const providerElement = await fixture(`
        &lt;${providerTag}&gt;
          &lt;${consumerTag}&gt;
          &lt;${consumerTag}&gt;
        
      `);

    const [consumerElement1, consumerElement2] = Array.from(
      providerElement.children as any,
    );

    expect(consumerElement1.contextValue).toBe(2);
    expect(consumerElement2.contextValue).toBe(2);
  });