<commit_before>import { expect } from 'chai';
import {mount, Wrapper} from 'avoriaz';
import {SubscriptionsComponent} from '../../../src/components/subscriptions/subscriptions';
import {MockServiceApi} from '../../../src/util/component-test';
import {spy, assert, stub} from 'sinon';
import {PushNotification} from '../../../src/components/subscriptions/helpers/push-notification';
import {FakePushApi} from '../util/fake-push-api';
import {PushSupport} from '../../../src/components/subscriptions/helpers/push-support';

let fakeSubscription: PushSubscription;
let pushSupportStub: PushSupport;

describe('Subscriptions component', () => {

  beforeEach(() => {
    fakeSubscription = <PushSubscription> {
      endpoint: <USVString> 'gcm/send/fakeSubscriptionId'
    };
    pushSupportStub = <PushSupport>{
      getNotificationPermission: () => { return 'denied'; },
      isPushManagerSupported: () => { return false; }
    };
  });

  it('should search an artist', async () => {
    const wrapper: Wrapper = mount(SubscriptionsComponent, { provide: {serviceApi: new MockServiceApi(spy())}});

    const searchInput: Wrapper = wrapper.find('input')[1];
    (<HTMLInputElement>searchInput.element).disabled = false;
    (<HTMLInputElement>searchInput.element).value = 'searchArtist';
    searchInput.trigger('input');

    expect(wrapper.vm.$data['artistsSearchResult']).to.equal(MockServiceApi.testArtists);
    expect(wrapper.find('.search-input > ul')[0].html()).to.equal('<ul><li>Alice</li><li>Bob</li></ul>');
  });

  it('should select an artist', async () => {
    const wrapper: Wrapper = mount(SubscriptionsComponent, { provide: {serviceApi: new MockServiceApi(spy())}});

    const searchInput: Wrapper = wrapper.find('input')[1];
    (<HTMLInputElement>searchInput.element).disabled = false;
    (<HTMLInputElement>searchInput.element).value = 'searchArtist';
    searchInput.trigger('input');

    // Select first artist option in dropdown:
    const option = wrapper.find('.search-input > ul > li')[0];
    option.trigger('click');

    expect(wrapper.vm.$data['buttonDisabled']).to.be.false;
    expect((<HTMLInputElement>wrapper.find('input')[1].element).value).to.equal('Alice');
  });

  it('should set UI according to current subscription', async () => {
    let push: PushNotification = new PushNotification(new FakePushApi(), pushSupportStub);
    stub(push, 'isPushSupported').returns(fakeSubscription);
    // stub(push, 'getCurrentSubscriptions').returns(MockServiceApi.testArtists);

    let wrapper: Wrapper = await mount(SubscriptionsComponent, {provide: {pushNotification: push}});

    expect(wrapper.vm.$data['pushEnabled']).to.be.true;
  });

  it('should display an error in push notifications are blocked by the user.', async () => {
    const push: PushNotification = new PushNotification(new FakePushApi(), pushSupportStub);
    let wrapper: Wrapper = await mount(SubscriptionsComponent, {provide: {pushNotification: push}});
    expect(wrapper.vm.$data['errorMessage']).to.equal('Push notifications are blocked.');
  });

  it('should add subscription for an artist', async () => {
    let push: PushNotification = new PushNotification(new FakePushApi(), pushSupportStub);
    stub(push, 'isPushSupported').returns(fakeSubscription);
    const saveSpy = spy(push, 'saveSubscription');

    let wrapper: Wrapper = await mount(SubscriptionsComponent, {provide: {serviceApi: new MockServiceApi(spy()), pushNotification: push}});

    const searchInput: Wrapper = wrapper.find('input')[1];
    (<HTMLInputElement>searchInput.element).disabled = false;
    (<HTMLInputElement>searchInput.element).value = 'searchArtist';
    searchInput.trigger('input');

    // Select first artist option in dropdown and click add button:
    wrapper.find('.search-input > ul > li')[0].trigger('click');
    wrapper.find('button')[0].trigger('click');

    assert.calledOnce(saveSpy);
    assert.calledWith(saveSpy, fakeSubscription, MockServiceApi.testArtists[0]);
  });

  it('should delete subscription for an artist', async () => {
    let push: PushNotification = new PushNotification(new FakePushApi(), pushSupportStub);
    stub(push, 'isPushSupported').returns(fakeSubscription);
    const deleteSpy = spy(push, 'deleteSubscription');

    let wrapper: Wrapper = await mount(SubscriptionsComponent, {provide: {serviceApi: new MockServiceApi(spy()), pushNotification: push}});
    wrapper.setData({currentSubscriptions: MockServiceApi.testArtists});
    wrapper.find('.subscriptions-list > li > button')[0].trigger('click');

    assert.calledOnce(deleteSpy);
    assert.calledWith(deleteSpy, fakeSubscription, MockServiceApi.testArtists[0]);
  });

  it('should change permissions', async () => {
    let push: PushNotification = new PushNotification(new FakePushApi(), pushSupportStub);
    const subscribeSpy = spy(push, 'subscribePush');

    let wrapper: Wrapper = await mount(SubscriptionsComponent, {provide: {serviceApi: new MockServiceApi(spy()), pushNotification: push}});
    wrapper.setData({pushEnabled: true});
    wrapper.find('input')[0].trigger('change');

    assert.calledOnce(subscribeSpy);
  });
});
<commit_msg>Testing unsubscription.<commit_after>import { expect } from 'chai';
import {mount, Wrapper} from 'avoriaz';
import {SubscriptionsComponent} from '../../../src/components/subscriptions/subscriptions';
import {MockServiceApi} from '../../../src/util/component-test';
import {spy, assert, stub, SinonStub} from 'sinon';
import {PushNotification} from '../../../src/components/subscriptions/helpers/push-notification';
import {FakePushApi} from '../util/fake-push-api';
import {PushSupport} from '../../../src/components/subscriptions/helpers/push-support';

let fakeSubscription: PushSubscription;
let pushSupportStub: PushSupport;
let confirmStub: SinonStub = stub(window, 'confirm').returns(true);

describe('Subscriptions component', () => {

  beforeEach(() => {
    fakeSubscription = <PushSubscription> {
      endpoint: <USVString> 'gcm/send/fakeSubscriptionId'
    };
    pushSupportStub = <PushSupport>{
      getNotificationPermission: () => { return 'denied'; },
      isPushManagerSupported: () => { return false; }
    };
  });

  it('should search an artist', async () => {
    const wrapper: Wrapper = mount(SubscriptionsComponent, { provide: {serviceApi: new MockServiceApi(spy())}});

    const searchInput: Wrapper = wrapper.find('input')[1];
    (<HTMLInputElement>searchInput.element).disabled = false;
    (<HTMLInputElement>searchInput.element).value = 'searchArtist';
    searchInput.trigger('input');

    expect(wrapper.vm.$data['artistsSearchResult']).to.equal(MockServiceApi.testArtists);
    expect(wrapper.find('.search-input > ul')[0].html()).to.equal('<ul><li>Alice</li><li>Bob</li></ul>');
  });

  it('should select an artist', async () => {
    const wrapper: Wrapper = mount(SubscriptionsComponent, { provide: {serviceApi: new MockServiceApi(spy())}});

    const searchInput: Wrapper = wrapper.find('input')[1];
    (<HTMLInputElement>searchInput.element).disabled = false;
    (<HTMLInputElement>searchInput.element).value = 'searchArtist';
    searchInput.trigger('input');

    // Select first artist option in dropdown:
    const option = wrapper.find('.search-input > ul > li')[0];
    option.trigger('click');

    expect(wrapper.vm.$data['buttonDisabled']).to.be.false;
    expect((<HTMLInputElement>wrapper.find('input')[1].element).value).to.equal('Alice');
  });

  it('should set UI according to current subscription', async () => {
    let push: PushNotification = new PushNotification(new FakePushApi(), pushSupportStub);
    stub(push, 'isPushSupported').returns(fakeSubscription);
    // stub(push, 'getCurrentSubscriptions').returns(MockServiceApi.testArtists);

    let wrapper: Wrapper = await mount(SubscriptionsComponent, {provide: {pushNotification: push}});

    expect(wrapper.vm.$data['pushEnabled']).to.be.true;
  });

  it('should display an error in push notifications are blocked by the user.', async () => {
    const push: PushNotification = new PushNotification(new FakePushApi(), pushSupportStub);
    let wrapper: Wrapper = await mount(SubscriptionsComponent, {provide: {pushNotification: push}});
    expect(wrapper.vm.$data['errorMessage']).to.equal('Push notifications are blocked.');
  });

  it('should add subscription for an artist', async () => {
    let push: PushNotification = new PushNotification(new FakePushApi(), pushSupportStub);
    stub(push, 'isPushSupported').returns(fakeSubscription);
    const saveSpy = spy(push, 'saveSubscription');

    let wrapper: Wrapper = await mount(SubscriptionsComponent, {provide: {serviceApi: new MockServiceApi(spy()), pushNotification: push}});

    const searchInput: Wrapper = wrapper.find('input')[1];
    (<HTMLInputElement>searchInput.element).disabled = false;
    (<HTMLInputElement>searchInput.element).value = 'searchArtist';
    searchInput.trigger('input');

    // Select first artist option in dropdown and click add button:
    wrapper.find('.search-input > ul > li')[0].trigger('click');
    wrapper.find('button')[0].trigger('click');

    assert.calledOnce(saveSpy);
    assert.calledWith(saveSpy, fakeSubscription, MockServiceApi.testArtists[0]);
  });

  it('should delete subscription for an artist', async () => {
    let push: PushNotification = new PushNotification(new FakePushApi(), pushSupportStub);
    stub(push, 'isPushSupported').returns(fakeSubscription);
    const deleteSpy = spy(push, 'deleteSubscription');

    let wrapper: Wrapper = await mount(SubscriptionsComponent, {provide: {serviceApi: new MockServiceApi(spy()), pushNotification: push}});
    wrapper.setData({currentSubscriptions: MockServiceApi.testArtists});
    wrapper.find('.subscriptions-list > li > button')[0].trigger('click');

    assert.calledOnce(deleteSpy);
    assert.calledWith(deleteSpy, fakeSubscription, MockServiceApi.testArtists[0]);
  });

  it('should change permissions', async () => {
    let push: PushNotification = new PushNotification(new FakePushApi(), pushSupportStub);
    const subscribeSpy = spy(push, 'subscribePush');

    let wrapper: Wrapper = await mount(SubscriptionsComponent, {provide: {serviceApi: new MockServiceApi(spy()), pushNotification: push}});
    wrapper.setData({pushEnabled: true});
    wrapper.find('input')[0].trigger('change');

    assert.calledOnce(subscribeSpy);
  });

  it('should unsubscribe for push notifications', async () => {
    let push: PushNotification = new PushNotification(new FakePushApi(), pushSupportStub);
    confirmStub.returns(true);
    const unsubscribeSpy = spy(push, 'unsubscribePush');

    let wrapper: Wrapper = await mount(SubscriptionsComponent, {provide: {serviceApi: new MockServiceApi(spy()), pushNotification: push}});
    wrapper.setData({pushEnabled: false});
    wrapper.find('input')[0].trigger('change');

    assert.calledOnce(unsubscribeSpy);
  });

  it('should not unsubscribe for push notifications if user cancels confirm dialog', async () => {
    let push: PushNotification = new PushNotification(new FakePushApi(), pushSupportStub);
    confirmStub.returns(false);
    const unsubscribeSpy = spy(push, 'unsubscribePush');

    let wrapper: Wrapper = await mount(SubscriptionsComponent, {provide: {serviceApi: new MockServiceApi(spy()), pushNotification: push}});
    wrapper.setData({pushEnabled: false});
    wrapper.find('input')[0].trigger('change');

    assert.notCalled(unsubscribeSpy);
  });
});

{'fraction_non_alphanumeric': 0.15508533611981887, 'fraction_numerical': 0.0022640195053988156, 'mean_word_length': 5.171413218699624, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 73, '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': '5096897', 'n_tokens_mistral': 3267, 'n_tokens_neox': 3214, 'n_words': 691}