Skip to content

Commit

Permalink
Fix feature usage specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bengotow committed Dec 1, 2017
1 parent 7d59c67 commit 1013578
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
62 changes: 30 additions & 32 deletions app/spec/stores/feature-usage-store-spec.es6
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ import IdentityStore from '../../src/flux/stores/identity-store';

describe('FeatureUsageStore', function featureUsageStoreSpec() {
beforeEach(() => {
this.oldIdent = IdentityStore._identity;
IdentityStore._identity = { id: 'foo' };
IdentityStore._identity.featureUsage = {
'is-usable': {
quota: 10,
period: 'monthly',
usedInPeriod: 8,
featureLimitName: 'Usable Group A',
},
'not-usable': {
quota: 10,
period: 'monthly',
usedInPeriod: 10,
featureLimitName: 'Unusable Group A',
this.fakeIdentity = {
id: 'foo',
featureUsage: {
'is-usable': {
quota: 10,
period: 'monthly',
usedInPeriod: 8,
featureLimitName: 'Usable Group A',
},
'not-usable': {
quota: 10,
period: 'monthly',
usedInPeriod: 10,
featureLimitName: 'Unusable Group A',
},
},
};
});

afterEach(() => {
IdentityStore._identity = this.oldIdent;
spyOn(IdentityStore, 'identity').andReturn(this.fakeIdentity);
spyOn(IdentityStore, 'saveIdentity').andCallFake(async ident => {
this.fakeIdentity = ident;
});
});

describe('isUsable', () => {
Expand All @@ -42,41 +43,38 @@ describe('FeatureUsageStore', function featureUsageStoreSpec() {
});
});

describe('_markFeatureUsed', () => {
describe('markUsed', () => {
beforeEach(() => {
spyOn(Actions, 'queueTask');
spyOn(IdentityStore, 'saveIdentity').andCallFake(ident => {
IdentityStore._identity = ident;
});
});

afterEach(() => {
TaskQueue._queue = [];
});

it('immediately increments the identity counter', () => {
const before = IdentityStore._identity.featureUsage['is-usable'].usedInPeriod;
FeatureUsageStore._markFeatureUsed('is-usable');
const after = IdentityStore._identity.featureUsage['is-usable'].usedInPeriod;
const before = this.fakeIdentity.featureUsage['is-usable'].usedInPeriod;
FeatureUsageStore.markUsed('is-usable');
const after = this.fakeIdentity.featureUsage['is-usable'].usedInPeriod;
expect(after).toEqual(before + 1);
});

it('queues a task to sync the optimistic changes to the server', () => {
FeatureUsageStore._markFeatureUsed('is-usable');
FeatureUsageStore.markUsed('is-usable');
expect(Actions.queueTask).toHaveBeenCalled();
});
});

describe('use feature', () => {
describe('markUsedOrUpgrade', () => {
beforeEach(() => {
spyOn(FeatureUsageStore, '_markFeatureUsed').andReturn(Promise.resolve());
spyOn(FeatureUsageStore, 'markUsed').andReturn(Promise.resolve());
spyOn(Actions, 'openModal');
});

it("marks the feature used if it's usable", async () => {
await FeatureUsageStore.markUsedOrUpgrade('is-usable');
expect(FeatureUsageStore._markFeatureUsed).toHaveBeenCalled();
expect(FeatureUsageStore._markFeatureUsed.callCount).toBe(1);
expect(FeatureUsageStore.markUsed).toHaveBeenCalled();
expect(FeatureUsageStore.markUsed.callCount).toBe(1);
});

describe('showing modal', () => {
Expand All @@ -90,7 +88,7 @@ describe('FeatureUsageStore', function featureUsageStoreSpec() {

it('resolves the modal if you upgrade', async () => {
setImmediate(() => {
IdentityStore._identity.featureUsage['not-usable'].quota = 10000;
this.fakeIdentity.featureUsage['not-usable'].quota = 10000;
FeatureUsageStore._onModalClose();
});
await FeatureUsageStore.markUsedOrUpgrade('not-usable', this.lexicon);
Expand All @@ -100,7 +98,7 @@ describe('FeatureUsageStore', function featureUsageStoreSpec() {

it('pops open a modal with the correct text', async () => {
setImmediate(() => {
IdentityStore._identity.featureUsage['not-usable'].quota = 10000;
this.fakeIdentity.featureUsage['not-usable'].quota = 10000;
FeatureUsageStore._onModalClose();
});
await FeatureUsageStore.markUsedOrUpgrade('not-usable', this.lexicon);
Expand Down
2 changes: 0 additions & 2 deletions app/src/flux/stores/feature-usage-store.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ class FeatureUsageStore extends MailspringStore {

markUsed(feature) {
const next = JSON.parse(JSON.stringify(IdentityStore.identity()));
console.log('Next:');
console.log(JSON.stringify(next));

if (next.featureUsage[feature]) {
next.featureUsage[feature].usedInPeriod += 1;
Expand Down

0 comments on commit 1013578

Please sign in to comment.