{"size": 4423, "ext": "js", "max_stars_repo_path": "tests/unit/services/firebase-message-test.js", "max_stars_repo_name": "Matt-Jensen/ember-service-worker-emberfire-messaging", "max_stars_repo_stars_event_min_datetime": "2017-09-06T16:26:27.000Z", "max_stars_repo_stars_event_max_datetime": "2019-06-27T15:51:06.000Z", "max_issues_repo_path": "tests/unit/services/firebase-message-test.js", "max_issues_repo_name": "Matt-Jensen/ember-service-worker-emberfire-messaging", "max_issues_repo_issues_event_min_datetime": "2017-11-17T20:39:36.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-23T06:19:40.000Z", "max_forks_repo_path": "tests/unit/services/firebase-message-test.js", "max_forks_repo_name": "Matt-Jensen/ember-service-worker-emberfire-messaging", "max_forks_repo_forks_event_min_datetime": "2017-11-18T06:50:27.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-21T22:07:37.000Z"}
{"max_stars_count": 12, "max_issues_count": 14, "max_forks_count": 7, "avg_line_length": 27.8176100629, "max_line_length": 107, "alphanum_fraction": 0.6147411259}
import { resolve } from "rsvp";
import { module, test } from "ember-qunit";
import { assign } from "@ember/polyfills";
import { setupTest } from "ember-qunit";

const mockMessaging = (msgConfig = {}) =>
  resolve(
    assign(
      {
        onTokenRefresh: () => {},
        onMessage: () => {}
      },
      msgConfig
    )
  );

module("Unit | Service | firebase message", function(hooks) {
  setupTest(hooks);

  test("it does not subscribe to Firebase messaging hooks in Fastboot", function(assert) {
    assert.expect(0);
    const service = this.owner.lookup("service:firebase-message");
    service.setProperties({
      _isFastboot: true,
      messaging: mockMessaging({
        onTokenRefresh: () => assert.ok(false),
        onMessage: () => assert.ok(false)
      }),
    });
    service.init();
  });

  test("it adds subscribers via `subscribe` method", function(assert) {
    const subscriber = () => {};
    const service = this.owner.lookup("service:firebase-message");
    service.setProperties({
      messaging: mockMessaging(),
      _subscribers: []
    });
    const index = service.subscribe(subscriber);
    assert.strictEqual(
      service._subscribers[index],
      subscriber,
      "added subscriber function"
    );
  });

  test("it removes subscribers via `unsubscribe` method", function(assert) {
    const subscriber = () => {};
    const service = this.owner.lookup("service:firebase-message");
    service.setProperties({
      messaging: mockMessaging(),
      _subscribers: [subscriber]
    });
    service.unsubscribe(subscriber);
    assert.strictEqual(
      service._subscribers.length,
      0,
      "removed subscriber function"
    );
  });

  test("it invokes all `onMessage` subscribers with message payload", function(assert) {
    assert.expect(1);

    const expected = {};
    const subscriber = actual =>
      assert.strictEqual(actual, expected, "invoked with payload");
    const service = this.owner.lookup("service:firebase-message");

    service.setProperties({
      _subscribers: [subscriber],
      messaging: mockMessaging({
        onMessage: cb => cb(expected) // trigger onMessage immediately
      })
    });

    service.init()
  });

  test("it requests permission and resolves token on initialize", function(assert) {
    assert.expect(2);

    const expected = "123";
    const service = this.owner.lookup("service:firebase-message");

    service.setProperties({
      messaging: mockMessaging(),
      getToken: () => resolve(expected),
      requestPermission: () => {
        assert.ok(true, "invoked `requestPermission`");
        return resolve();
      },
      serviceWorkerReady: () => resolve()
    });

    service.initialize().then(actual => {
      assert.strictEqual(actual, expected, "resolved a token");
    });
  });

  test("it rejects promise chain when request for token resolves nothing on initialize", function(assert) {
    assert.expect(1);

    const service = this.owner.lookup("service:firebase-message");
    service.setProperties({
      mocking: mockMessaging({
        getToken: () => resolve("") // resolve falsey token
      }),
      requestPermission: () => resolve(),
      serviceWorkerReady: () => resolve()
    });

    return service
      .initialize()
      .then(() => assert.ok(false, "should not accept invalid token"))
      .catch(() => assert.ok(true, "rejected invalid token"));
  });

  test("it sets a token when a request for a token is successful", function(assert) {
    assert.expect(1);

    const expected = "123";

    const service = this.owner.lookup("service:firebase-message");
    service.setProperties({
      messaging: mockMessaging({
        getToken: () => resolve(expected)
      })
    });

    service
      .getToken()
      .then(() =>
        assert.strictEqual(service.get("token"), expected, "set resolved token")
      );
  });

  test("it destroys any local token and requests a new one onTokenRefesh", function(assert) {
    const service = this.owner.lookup("service:firebase-message");
    service.setProperties({
      token: "123",
      messaging: mockMessaging({
        onTokenRefresh: cb => cb.call(service) // invoke immediately
      }),
      getToken: () => {
        assert.strictEqual(
          service.token,
          "",
          "set local token to empty string"
        );
        return resolve("");
      }
    });

    service.init();
  });
});
