Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Using store prevents stencil test from being run #342

Open
3 tasks done
pimmesz opened this issue Jul 25, 2023 · 0 comments
Open
3 tasks done

bug: Using store prevents stencil test from being run #342

pimmesz opened this issue Jul 25, 2023 · 0 comments
Labels

Comments

@pimmesz
Copy link

pimmesz commented Jul 25, 2023

Prerequisites

Stencil Store Version

2.0.9

Stencil Version

4.0.2

Current Behavior

I have implemented a basic store in a stencil test project. Whenever I try to set store values en reset them in my test; the test loses the context and cannot find the component anymore. If I run a single test instead of both, it does not have any issue.

my-component.tsx

import { Component, State, h } from '@stencil/core';
import { store, onStoreChange } from '../../store/store';

@Component({
  tag: 'my-component',
  styleUrl: 'my-component.css',
  shadow: true,
})
export class MyComponent {
  @State() test = true;

  setTestState() {
    this.test = !this.test;
  }

  componentWillLoad() {
    onStoreChange('isActive', this.setTestState.bind(this));
  }

  render() {
    return <div>Hello, World! I'm</div>;
  }
}

my-component.spec.ts

import { newSpecPage } from '@stencil/core/testing';
import { MyComponent } from './my-component';
import { store } from '../../store/store';

describe('my-component', () => {
   afterEach(() => {
    store.dispose();
  });

  it('renders', async () => {
    const { root } = await newSpecPage({
      components: [MyComponent],
      html: '<my-component></my-component>',
    });

    store.state.isActive = false;

    expect(root).toEqualHtml(`
      <my-component>
        <mock:shadow-root>
          <div>
            Hello, World! I'm
          </div>
        </mock:shadow-root>
      </my-component>
    `);
  });
  
  it('renders', async () => {
    const { root } = await newSpecPage({
      components: [MyComponent],
      html: '<my-component></my-component>',
    });

    store.state.isActive = false;

    expect(root).toEqualHtml(`
      <my-component>
        <mock:shadow-root>
          <div>
            Hello, World! I'm
          </div>
        </mock:shadow-root>
      </my-component>
    `);
  });
});

store.ts

import { createStore } from '@stencil/store';

export interface StoreState {
  isActive: boolean;
}

const store = createStore<StoreState>({
  isActive: true
});

const onStoreChange = (key: keyof StoreState, cb: (value: string | boolean) => void): void => {
  store.onChange(key, (value: any) => {
    cb(value);
  });
};

export { store, onStoreChange };

Current error
TypeError: Cannot read properties of undefined (reading '$instanceValues$')

Expected Behavior

Run both tests without any issues.

Steps to Reproduce

With the code provided in the Current behavior box the failure could be reproduced.

Code Reproduction URL

https://github.com/pimmesz/stencil-test

Additional Information

No response

@ionitron-bot ionitron-bot bot added the triage label Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant