Commit 744c62ad authored by Andrei Oprea's avatar Andrei Oprea
Browse files

Bug 1714449 - Feature API does not fire update event on startup r=k88hudson

parent 628a0fba
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -98,6 +98,16 @@ class ExperimentStore extends SharedDataMap {
    super(sharedDataKey || DEFAULT_STORE_ID, options);
  }

  async init() {
    await super.init();

    this.getAllActive().forEach(experiment => {
      experiment.featureIds?.forEach(feature =>
        this._emitFeatureUpdate(feature, "feature-experiment-loaded")
      );
    });
  }

  /**
   * Given a feature identifier, find an active experiment that matches that feature identifier.
   * This assumes, for now, that there is only one active experiment per feature per browser.
+30 −0
Original line number Diff line number Diff line
@@ -523,3 +523,33 @@ add_task(async function test_isEnabled_backwards_compatible() {
  );
  Assert.ok(exposureSpy.calledOnce, "Exposure event sent");
});

add_task(async function test_onUpdate_before_store_ready() {
  let sandbox = sinon.createSandbox();
  const feature = new ExperimentFeature("foo", FAKE_FEATURE_MANIFEST);
  const stub = sandbox.stub();
  const manager = ExperimentFakes.manager();
  sandbox.stub(ExperimentAPI, "_store").get(() => manager.store);
  sandbox.stub(manager.store, "getAllActive").returns([
    ExperimentFakes.experiment("foo-experiment", {
      featureIds: ["foo"],
      branch: { slug: "control", feature: { featureId: "foo", value: null } },
    }),
  ]);

  // We register for updates before the store finished loading experiments
  // from disk
  feature.onUpdate(stub);

  await manager.onStartup();

  Assert.ok(
    stub.calledOnce,
    "Called on startup after loading experiments from disk"
  );
  Assert.equal(
    stub.firstCall.args[1],
    "feature-experiment-loaded",
    "Called for the expected reason"
  );
});