<commit_before>import { moduleFor, test } from 'ember-qunit';
import sinon from 'sinon';
import EmberObject from '@ember/object';

let sandbox, config, instanceStub, identity;

moduleFor('ember-metrics@metrics-adapter:amplitude', 'amplitude adapter', {
  beforeEach() {
    sandbox = sinon.createSandbox();
    config = {
      apiKey: 'AMPLITUDE_KEY'
    };
    instanceStub = EmberObject.create({
      identify: sinon.fake(),
      setUserId: sinon.fake(),
      logEvent: sinon.fake(),
      setOptOut: sinon.fake()
    });
    identity = EmberObject.create();
  },
  afterEach() {
    sandbox.restore();
  }
});

test('#identify sets the distinct user ID, and calls amplitude with any extra user properties', function(assert) {
  const adapter = this.subject({ config });

  sandbox.replace(window.amplitude, 'getInstance', () => instanceStub);
  sandbox.replace(window.amplitude, 'Identify', () => identity);

  adapter.identify({
    distinctId: '123',
    email: '<EMAIL>'
  });

  assert.ok(instanceStub.setUserId.calledWith('123'), 'it calls setUserId with distinctId');
  assert.ok(instanceStub.identify.calledWith(identity), 'it sends the correct user identity');
  assert.ok(instanceStub.logEvent.calledWith('Identify'), 'it calls a logEvent for "Identify"');

  assert.equal(identity.get('email'), '<EMAIL>', 'extra user properties are correctly assigned to the identity');
});

test('#trackEvent sends the correct request shape', function(assert) {
  const adapter = this.subject({ config });
  sandbox.replace(window.amplitude, 'getInstance', () => instanceStub);

  adapter.trackEvent({
    event: 'Registered User',
    email: '<EMAIL>'
  });
  const expectedEventProperties = {
    email: '<EMAIL>'
  };

  assert.ok(instanceStub.logEvent.calledWith('Registered User', expectedEventProperties), 'track called with proper arguments');
});

test('#trackPage returns the correct response shape', function(assert) {
  const adapter = this.subject({ config });
  sandbox.replace(window.amplitude, 'getInstance', () => instanceStub);

  const pageProperties = {
    url: '/shot-charts/missed-threes'
  };
  adapter.trackPage(pageProperties);

  assert.ok(instanceStub.logEvent.calledWith('Page View', pageProperties), 'track called with proper arguments');
});

test('#optOut calls amplitude optOut with false', function(assert) {
  const adapter = this.subject({ config });
  sandbox.replace(window.amplitude, 'getInstance', () => instanceStub);

  adapter.optOut();

  assert.ok(instanceStub.setOptOut.calledWith(true), 'optOut called with correct arguments');
});

test('#optIn calls amplitude optOut with false', function(assert) {
  const adapter = this.subject({ config });
  sandbox.replace(window.amplitude, 'getInstance', () => instanceStub);

  adapter.optIn();

  assert.ok(instanceStub.setOptOut.calledWith(false), 'optOut called with correct arguments');
});
<commit_msg>convert to module tests instead of moduleFor<commit_after>import { module, test } from 'ember-qunit';
import sinon from 'sinon';
import EmberObject from '@ember/object';

module('amplitude adapter', function(hooks) {
  hooks.beforeEach(function () {
    this.sandbox = sinon.createSandbox();
    this.config = {
      apiKey: 'AMPLITUDE_KEY'
    };
    this.instanceStub = EmberObject.create({
      identify: sinon.fake(),
      setUserId: sinon.fake(),
      logEvent: sinon.fake(),
      setOptOut: sinon.fake()
    });
    this.identity = EmberObject.create();

    this.subject = this.owner.factoryFor('ember-metrics@metrics-adapter:amplitude').create({ config: this.config });
  });

  hooks.afterEach(function () {
    this.sandbox.restore();
  });

  test('#identify sets the distinct user ID, and calls amplitude with any extra user properties', function(assert) {

    this.sandbox.replace(window.amplitude, 'getInstance', () => this.instanceStub);
    this.sandbox.replace(window.amplitude, 'Identify', () => this.identity);

    this.adapter.identify({
      distinctId: '123',
      email: '<EMAIL>'
    });

    assert.ok(this.instanceStub.setUserId.calledWith('123'), 'it calls setUserId with distinctId');
    assert.ok(this.instanceStub.identify.calledWith(this.identity), 'it sends the correct user identity');
    assert.ok(this.instanceStub.logEvent.calledWith('Identify'), 'it calls a logEvent for "Identify"');

    assert.equal(this.identity.get('email'), '<EMAIL>', 'extra user properties are correctly assigned to the identity');
  });

  test('#trackEvent sends the correct request shape', function(assert) {
    this.sandbox.replace(window.amplitude, 'getInstance', () => this.instanceStub);

    this.adapter.trackEvent({
      event: 'Registered User',
      email: '<EMAIL>'
    });
    const expectedEventProperties = {
      email: '<EMAIL>'
    };

    assert.ok(this.instanceStub.logEvent.calledWith('Registered User', expectedEventProperties), 'track called with proper arguments');
  });

  test('#trackPage returns the correct response shape', function(assert) {
    this.sandbox.replace(window.amplitude, 'getInstance', () => this.instanceStub);

    const pageProperties = {
      url: '/shot-charts/missed-threes'
    };
    this.adapter.trackPage(pageProperties);

    assert.ok(this.instanceStub.logEvent.calledWith('Page View', pageProperties), 'track called with proper arguments');
  });

  test('#optOut calls amplitude optOut with false', function(assert) {
    this.sandbox.replace(window.amplitude, 'getInstance', () => this.instanceStub);

    this.adapter.optOut();

    assert.ok(this.instanceStub.setOptOut.calledWith(true), 'optOut called with correct arguments');
  });

  test('#optIn calls amplitude optOut with false', function(assert) {
    this.sandbox.replace(window.amplitude, 'getInstance', () => this.instanceStub);

    this.adapter.optIn();

    assert.ok(this.instanceStub.setOptOut.calledWith(false), 'optOut called with correct arguments');
  });
});


{'fraction_non_alphanumeric': 0.14768502872592093, 'fraction_numerical': 0.002027712064886786, 'mean_word_length': 4.547328959700094, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, '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': '3098200', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1724, 'n_words': 391}