<commit_before>/* global describe, it, jest */
import preact from 'preact';
import { expect } from 'chai';
import {mapper, default as Container} from './container';
import createStore from './store';

describe('Container', () => {
  it('re-renders when the store changes', () => {
    const store = createStore();
    const childComponent = jest.fn();
    preact.render(
      <Container store={store}>
        {() => childComponent()}
      </Container>,
      document.body
    );
    expect(childComponent.mock.calls).to.have.length(1);
    store.set('mount', {a: 1});
    expect(childComponent.mock.calls).to.have.length(2);
  });

  describe('mapper', () => {
    it('maps state to props and actions', () => {
      const store = createStore();
      store.set('mount', {a: 1, b: 2});

      const action = jest.fn();

      const childComponent = ({prop1, prop2, onAction}) => {
        expect(prop1).to.equal(1);
        expect(prop2).to.equal(2);
        onAction(5);
        expect(action.mock.calls[0][0]).to.equal(store);
        expect(action.mock.calls[0][1]).to.equal(5);
      };

      const Component = mapper(
        {
          prop1: store => store.get('mount').a,
          prop2: store => store.get('mount').b
        },
        {onAction: action}
      )(childComponent);

      preact.render(
        <Container store={store}>{
          store => <Component store={store}/>
        }</Container>,
        document.body
      );
    });
  });
});<commit_msg>test to prove rendering skip when relevant store tree hasnt changed<commit_after>/* global describe, it, jest */
import preact from 'preact';
import { expect } from 'chai';
import {mapper, default as Container} from './container';
import createStore from './store';

describe('Container', () => {
  it('re-renders when the store changes', () => {
    const store = createStore();
    const childComponent = jest.fn();
    preact.render(
      <Container store={store}>
        {() => childComponent()}
      </Container>,
      document.body
    );
    expect(childComponent.mock.calls).to.have.length(1);
    store.set('mount', {a: 1});
    expect(childComponent.mock.calls).to.have.length(2);
  });

  describe('mapper', () => {
    it('maps state to props and actions', () => {
      const store = createStore();
      store.set('mount', {a: 1, b: 2});

      const action = jest.fn();

      const childComponent = ({prop1, prop2, onAction}) => {
        expect(prop1).to.equal(1);
        expect(prop2).to.equal(2);
        onAction(5);
        expect(action.mock.calls[0][0]).to.equal(store);
        expect(action.mock.calls[0][1]).to.equal(5);
      };

      const Component = mapper(
        {
          prop1: store => store.get('mount').a,
          prop2: store => store.get('mount').b
        },
        {onAction: action}
      )(childComponent);

      preact.render(
        <Container store={store}>{
          store => <Component store={store}/>
        }</Container>,
        document.body
      );
    });

    it('skips rendering if the relevant section of the store does not change', () => {
      const store = createStore({
        mount1: {a: 1},
        mount2: {b: 2}
      });

      const childComponent = jest.fn();

      const Component = mapper(
        {
          prop1: store => store.get('mount1').a
        }
      )(childComponent);

      preact.render(
        <Container store={store}>{
          store => <Component store={store}/>
        }</Container>,
        document.body
      );
      expect(childComponent.mock.calls).to.have.length(1);
      store.set('mount2', {b: 3});
      expect(childComponent.mock.calls).to.have.length(1);
      store.set('mount1', {a: 2});
      expect(childComponent.mock.calls).to.have.length(2);
    });
  });
});
{'fraction_non_alphanumeric': 0.1761122881355932, 'fraction_numerical': 0.01350635593220339, 'mean_word_length': 2.427404718693285, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3683760', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1143, 'n_words': 248}