<commit_before>import * as Bacon from "..";
import { expect } from "chai";

import { expectStreamEvents, expectPropertyEvents, series, semiunstable, unstable, repeatedly, fromArray, later, once, t, activate, deferred } from "./util/SpecHelper";

function latter<A> (a: A, b: A) { return b }
const concat = (x: string, y: string) => x + y
type Faq = {faq: string}

describe("Integration tests", function() {
  describe("Property.sampleBy", function() {
    describe("uses updated property after combine", function() {
      return expectPropertyEvents(
        function() {
          const src = series(2, ["b", "c"]).toProperty("a");
          const combined = Bacon.constant("").combine(src, latter);
          return (<any>src).sampledBy(combined, concat);
        },
        ["aa", "bb", "cc"]);
    });
    return describe("With circular Bus setup", () =>
      it("Just works (bug fix)", function() {
        const values: boolean[] = [];
        const clicks = new Bacon.Bus<boolean>();
        const toggleBus = new Bacon.Bus<boolean>();

        const shown = toggleBus.toProperty(false);
        shown.changes().onValue(show => { values.push(show) });
        const toggleClicks = shown.sampledBy(clicks).map(shown => !shown);

        toggleBus.plug(toggleClicks);

        clicks.push(true);
        return expect(values).to.deep.equal([true]);
      })
    );
  });
  describe("uses updated property after combine with subscriber", function() {
    return expectPropertyEvents(
      function() {
        const src = series(2, ["b", "c"]).toProperty("a");
        const combined = Bacon.constant("").combine(src, latter);
        combined.onValue();
        return (<any>src).sampledBy(combined, concat);
      },
      ["aa", "bb", "cc"]);
  });
  describe("Property.skipDuplicates", () =>
    describe("Doesn't skip initial value (bug fix #211)", function() {
      const b = new Bacon.Bus();
      const p = b.toProperty();
      p.onValue(function() {}); // force property update
      const s = p.skipDuplicates();
      b.push('foo');

      describe("series 1", () => expectPropertyEvents((() => s.take(1)), ["foo"]));
      describe("series 2", () => expectPropertyEvents((() => s.take(1)), ["foo"]));
      return describe("series 3", () => expectPropertyEvents((() => s.take(1)), ["foo"]));
    })
  );
  describe("EventStream.skipDuplicates", () =>
    it("Drops duplicates with subscribers with non-overlapping subscription time (#211)", function() {
      const b = new Bacon.Bus<number>();
      const noDups = b.skipDuplicates();
      const round = function(expected: number[]) {
        const values: number[] = [];
        noDups.take(1).onValue(x => { values.push(x) });
        b.push(1);
        return expect(values).to.deep.equal(expected);
      };
      round([1]);
      round([]);
      return round([]);
    })
  );
  describe("EventStream.concat", () =>
    describe("Works with synchronized left stream and doAction", () =>
      expectStreamEvents(
        function() {
          const bus = new Bacon.Bus();
          const stream = fromArray([1,2]).flatMapLatest(x => Bacon.once(x).concat(later(10, x).doAction(function(x) { bus.push(x); return bus.end(); })));
          stream.onValue(function() {});
          return bus;
        },
        [2])
    )
  );
  describe("EventStream.flatMap", () =>
    describe("works with a complex setup (fix #363)", function() {
      it("case 1 (samplee has no subscribers)", function() {
        let result: Faq|undefined = undefined;
        const prop: Bacon.Property<Faq> = Bacon.combineTemplate({faq: later(1).toProperty("default value")});
        Bacon.once(0).flatMap(function(_) {
            const problem = prop.sampledBy(Bacon.once(0));
            return problem.onValue(x => { result = x });
        }).onValue(function() {});
        return deferred(() => expect(result).to.deep.equal({faq: "default value"}));
      });
      it("case 2 (samplee has subscriber)", function() {
        let result: Faq|undefined = undefined;
        const prop: Bacon.Property<Faq> = Bacon.combineTemplate({faq: later(1).toProperty("default value")});
        prop.onValue(function() {});
        Bacon.once(0).flatMap(function(_) {
            const problem = prop.sampledBy(Bacon.once(0));
            return problem.onValue(x => { result = x });
        }).onValue(function() {});
        return deferred(() => expect(result).to.deep.equal({faq: "default value"}));
      });
      return it("case 3 (original fiddle)", function() {
        let result: Faq|undefined = undefined;
        const input = new Bacon.Bus();
        const prop: Bacon.Property<Faq> = Bacon.combineTemplate({faq: input.toProperty("default value")});
        const events = new Bacon.Bus<number>();
        events.flatMapLatest(function(_) {
            const problem = prop.sampledBy(Bacon.once(0));
            return problem.onValue(x => { result = x });
        }).onValue(function() {});
        events.push(0);
        return deferred(() => expect(result).to.deep.equal({faq: "default value"}));
      });
    })
  );
  describe("Property.flatMap", () =>
    describe("works in a complex scenario #338", () =>
      expectStreamEvents(
        function() {
          const a = activate(series(2, ["a", "A"]));
          const b = activate(series(2, ["b", "B"])).delay(1).toProperty();<commit_msg>typing fix<commit_after>import * as Bacon from "..";
import { expect } from "chai";

import { expectStreamEvents, expectPropertyEvents, series, semiunstable, unstable, repeatedly, fromArray, later, once, t, activate, deferred } from "./util/SpecHelper";

function latter<A> (a: A, b: A) { return b }
const concat = (x: string, y: string) => x + y
type Faq = {faq: string}

describe("Integration tests", function() {
  describe("Property.sampleBy", function() {
    describe("uses updated property after combine", function() {
      return expectPropertyEvents(
        function() {
          const src = series(2, ["b", "c"]).toProperty("a");
          const combined = Bacon.constant("").combine(src, latter);
          return (<any>src).sampledBy(combined, concat);
        },
        ["aa", "bb", "cc"]);
    });
    return describe("With circular Bus setup", () =>
      it("Just works (bug fix)", function() {
        const values: boolean[] = [];
        const clicks = new Bacon.Bus<boolean>();
        const toggleBus = new Bacon.Bus<boolean>();

        const shown = toggleBus.toProperty(false);
        shown.changes().onValue(show => { values.push(show) });
        const toggleClicks = shown.sampledBy(clicks).map(shown => !shown);

        toggleBus.plug(toggleClicks);

        clicks.push(true);
        return expect(values).to.deep.equal([true]);
      })
    );
  });
  describe("uses updated property after combine with subscriber", function() {
    return expectPropertyEvents(
      function() {
        const src = series(2, ["b", "c"]).toProperty("a");
        const combined = Bacon.constant("").combine(src, latter);
        combined.onValue();
        return (<any>src).sampledBy(combined, concat);
      },
      ["aa", "bb", "cc"]);
  });
  describe("Property.skipDuplicates", () =>
    describe("Doesn't skip initial value (bug fix #211)", function() {
      const b = new Bacon.Bus();
      const p = b.toProperty();
      p.onValue(function() {}); // force property update
      const s = p.skipDuplicates();
      b.push('foo');

      describe("series 1", () => expectPropertyEvents((() => s.take(1)), ["foo"]));
      describe("series 2", () => expectPropertyEvents((() => s.take(1)), ["foo"]));
      return describe("series 3", () => expectPropertyEvents((() => s.take(1)), ["foo"]));
    })
  );
  describe("EventStream.skipDuplicates", () =>
    it("Drops duplicates with subscribers with non-overlapping subscription time (#211)", function() {
      const b = new Bacon.Bus<number>();
      const noDups = b.skipDuplicates();
      const round = function(expected: number[]) {
        const values: number[] = [];
        noDups.take(1).onValue(x => { values.push(x) });
        b.push(1);
        return expect(values).to.deep.equal(expected);
      };
      round([1]);
      round([]);
      return round([]);
    })
  );
  describe("EventStream.concat", () =>
    describe("Works with synchronized left stream and doAction", () =>
      expectStreamEvents(
        function() {
          const bus = new Bacon.Bus();
          const stream = fromArray([1,2]).flatMapLatest(x => Bacon.once(x).concat(later(10, x).doAction(function(x) { bus.push(x); return bus.end(); })));
          stream.onValue(function() {});
          return bus;
        },
        [2])
    )
  );
  describe("EventStream.flatMap", () =>
    describe("works with a complex setup (fix #363)", function() {
      it("case 1 (samplee has no subscribers)", function() {
        let result: Faq|undefined = undefined;
        const prop: Bacon.Property<Faq> = Bacon.combineTemplate({faq: later(1).toProperty("default value")});
        Bacon.once(0).flatMap(function(_) {
            const problem = prop.sampledBy(Bacon.once(0));
            return problem.onValue(x => { result = x });
        }).onValue(function() {});
        return deferred(() => expect(result).to.deep.equal({faq: "default value"}));
      });
      it("case 2 (samplee has subscriber)", function() {
        let result: Faq|undefined = undefined;
        const prop: Bacon.Property<Faq> = Bacon.combineTemplate({faq: later(1).toProperty("default value")});
        prop.onValue(function() {});
        Bacon.once(0).flatMap(function(_) {
            const problem = prop.sampledBy(Bacon.once(0));
            return problem.onValue(x => { result = x });
        }).onValue(function() {});
        return deferred(() => expect(result).to.deep.equal({faq: "default value"}));
      });
      return it("case 3 (original fiddle)", function() {
        let result: Faq|undefined = undefined;
        const input = new Bacon.Bus<string>();
        const prop: Bacon.Property<Faq> = Bacon.combineTemplate({faq: input.toProperty("default value")});
        const events = new Bacon.Bus<number>();
        events.flatMapLatest(function(_) {
            const problem = prop.sampledBy(Bacon.once(0));
            return problem.onValue(x => { result = x });
        }).onValue(function() {});
        events.push(0);
        return deferred(() => expect(result).to.deep.equal({faq: "default value"}));
      });
    })
  );
  describe("Property.flatMap", () =>
    describe("works in a complex scenario #338", () =>
      expectStreamEvents(
        function() {
          const a = activate(series(2, ["a", "A"]));
          const b = activate(series(2, ["b", "B"])).delay(1).toProperty();
{'fraction_non_alphanumeric': 0.18044552148382886, 'fraction_numerical': 0.007829247832976047, 'mean_word_length': 2.9046579330422126, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 74, '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': '6220779', 'n_tokens_mistral': 3237, 'n_tokens_neox': 3106, 'n_words': 724}