<commit_before>/* @flow */

import merge from './merge';
import LiveSet from '.';
import delay from 'pdelay';

test('works', async () => {
  const ls1Cleanup = jest.fn();
  const ls1 = new LiveSet({
    read: () => new Set([{x:'1'}, {x:'one'}]),
    listen(setValues, controller) {
      setValues(this.read());
      const originalValues = Array.from(ls1.values());
      controller.add({x:'uno'});
      setTimeout(() => {
        controller.remove(originalValues[0]);
        controller.add({x:'ten'});
      }, 30);
      return ls1Cleanup;
    }
  });

  const ls2Cleanup = jest.fn();
  const ls2 = new LiveSet({
    read: () => new Set([{x:'2'}, {x:'two'}]),
    listen(setValues, controller) {
      setValues(this.read());
      const originalValues = Array.from(ls2.values());
      controller.add({x:'dos'});
      setTimeout(() => {
        controller.remove(originalValues[0]);
        controller.add({x:'twenty'});
      }, 90);
      return ls2Cleanup;
    }
  });

  const ls = merge([ls1, ls2]);

  expect(Array.from(ls.values())).toEqual([{x:'1'}, {x:'one'}, {x:'2'}, {x:'two'}]);

  const next = jest.fn(), error = jest.fn(), complete = jest.fn();
  const sub = ls.subscribe({next, error, complete});
  expect(Array.from(ls.values())).toEqual([{x:'1'}, {x:'one'}, {x:'uno'}, {x:'2'}, {x:'two'}, {x:'dos'}]);
  expect(next.mock.calls).toEqual([]);

  await delay(60);

  expect(Array.from(ls.values())).toEqual([{x:'one'}, {x:'uno'}, {x:'2'}, {x:'two'}, {x:'dos'}, {x:'ten'}]);
  expect(next.mock.calls).toEqual([
    [[{type:'remove', value:{x:'1'}}, {type:'add', value:{x:'ten'}}]],
  ]);

  await delay(60);

  expect(Array.from(ls.values())).toEqual([{x:'one'}, {x:'uno'}, {x:'two'}, {x:'dos'}, {x:'ten'}, {x:'twenty'}]);

  expect(next.mock.calls).toEqual([
    [[{type:'remove', value:{x:'1'}}, {type:'add', value:{x:'ten'}}]],
    [[{type:'remove', value:{x:'2'}}, {type:'add', value:{x:'twenty'}}]],
  ]);
  expect(error).toHaveBeenCalledTimes(0);
  expect(complete).toHaveBeenCalledTimes(0);
  expect(ls1Cleanup).toHaveBeenCalledTimes(0);<commit_msg>Make merge test a little more resilient.<commit_after>/* @flow */

import merge from './merge';
import LiveSet from '.';
import delay from 'pdelay';

test('works', async () => {
  const ls1Cleanup = jest.fn();
  const ls1 = new LiveSet({
    read: () => new Set([{x:'1'}, {x:'one'}]),
    listen(setValues, controller) {
      setValues(this.read());
      const originalValues = Array.from(ls1.values());
      controller.add({x:'uno'});
      setTimeout(() => {
        controller.remove(originalValues[0]);
        controller.add({x:'ten'});
      }, 50);
      return ls1Cleanup;
    }
  });

  const ls2Cleanup = jest.fn();
  const ls2 = new LiveSet({
    read: () => new Set([{x:'2'}, {x:'two'}]),
    listen(setValues, controller) {
      setValues(this.read());
      const originalValues = Array.from(ls2.values());
      controller.add({x:'dos'});
      setTimeout(() => {
        controller.remove(originalValues[0]);
        controller.add({x:'twenty'});
      }, 150);
      return ls2Cleanup;
    }
  });

  const ls = merge([ls1, ls2]);

  expect(Array.from(ls.values())).toEqual([{x:'1'}, {x:'one'}, {x:'2'}, {x:'two'}]);

  const next = jest.fn(), error = jest.fn(), complete = jest.fn();
  const sub = ls.subscribe({next, error, complete});
  expect(Array.from(ls.values())).toEqual([{x:'1'}, {x:'one'}, {x:'uno'}, {x:'2'}, {x:'two'}, {x:'dos'}]);
  expect(next.mock.calls).toEqual([]);

  await delay(100);

  expect(Array.from(ls.values())).toEqual([{x:'one'}, {x:'uno'}, {x:'2'}, {x:'two'}, {x:'dos'}, {x:'ten'}]);
  expect(next.mock.calls).toEqual([
    [[{type:'remove', value:{x:'1'}}, {type:'add', value:{x:'ten'}}]],
  ]);

  await delay(100);

  expect(Array.from(ls.values())).toEqual([{x:'one'}, {x:'uno'}, {x:'two'}, {x:'dos'}, {x:'ten'}, {x:'twenty'}]);

  expect(next.mock.calls).toEqual([
    [[{type:'remove', value:{x:'1'}}, {type:'add', value:{x:'ten'}}]],
    [[{type:'remove', value:{x:'2'}}, {type:'add', value:{x:'twenty'}}]],
  ]);
  expect(error).toHaveBeenCalledTimes(0);
  expect(complete).toHaveBeenCalledTimes(0);
  expect(ls1Cleanup).toHaveBeenCalledTimes(0);
{'fraction_non_alphanumeric': 0.2680535628885701, 'fraction_numerical': 0.016977522716403636, 'mean_word_length': 4.176980198019802, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '3259542', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1492, 'n_words': 251}