mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-11-14 21:57:55 +08:00
e638e94084
Summary: This is a WIP Depends on D3799 on billing.nylas.com This adds a `FeatureUsageStore` which determines whether a feature can be used or not. It also allows us to record "using" a feature. Feature Usage is ultimately backed by the Nylas Identity and cached locally in the Identity object. Since feature usage is attached to the Nylas Identity, we move the whole Identity object (except for the ID) to the database. This includes a migration (with tests!) to move the Nylas Identity from the config into the Database. We still, however, need the Nylas ID to stay in the config so it can be synchronously accessed by the /browser process on bootup when determining what windows to show. It's also convenient to know what the Nylas ID is by looking at the config. There's logic (with tests!) to make sure these stay in sync. If you delete the Nylas ID from the config, it'll be the same as logging you out. The schema for the feature usage can be found in more detail on D3799. By the time it reaches Nylas Mail, the Nylas ID object has a `feature_usage` attribute that has each feature (keyed by the feature name) and information about the plans attached to it. The schema Nylas Mail sees looks like: ``` "feature_usage": { "snooze": { quota: 10, peroid: 'monthly', used_in_period: 8, feature_limit_name: 'Snooze Group A', }, } ``` See D3799 for more info about how these are generated. One final change that's in here is how Stores are loaded. Most of our core stores are loaded at require time, but now things like the IdentityStore need to do asynchronous things on activation. In reality most of our stores do this and it's a miracle it hasn't caused more problems! Now when stores activate we optionally look for an `activate` method and `await` for it. This was necessary so downstream classes (like the Onboarding Store), see a fully initialized IdentityStore by the time it's time to use them Test Plan: New tests! Reviewers: khamidou, juan, halla Reviewed By: juan Differential Revision: https://phab.nylas.com/D3808
82 lines
2.6 KiB
JavaScript
82 lines
2.6 KiB
JavaScript
import {TaskQueueStatusStore} from 'nylas-exports'
|
|
import FeatureUsageStore from '../../src/flux/stores/feature-usage-store'
|
|
import Task from '../../src/flux/tasks/task'
|
|
import SendFeatureUsageEventTask from '../../src/flux/tasks/send-feature-usage-event-task'
|
|
import IdentityStore from '../../src/flux/stores/identity-store'
|
|
|
|
describe("FeatureUsageStore", function featureUsageStoreSpec() {
|
|
beforeEach(() => {
|
|
this.oldIdent = IdentityStore._identity;
|
|
IdentityStore._identity = {id: 'foo'}
|
|
IdentityStore._identity.feature_usage = {
|
|
"is-usable": {
|
|
quota: 10,
|
|
peroid: 'monthly',
|
|
used_in_period: 8,
|
|
feature_limit_name: 'Usable Group A',
|
|
},
|
|
"not-usable": {
|
|
quota: 10,
|
|
peroid: 'monthly',
|
|
used_in_period: 10,
|
|
feature_limit_name: 'Unusable Group A',
|
|
},
|
|
}
|
|
});
|
|
|
|
afterEach(() => {
|
|
IdentityStore._identity = this.oldIdent
|
|
});
|
|
|
|
describe("isUsable", () => {
|
|
it("returns true if a feature hasn't met it's quota", () => {
|
|
expect(FeatureUsageStore.isUsable("is-usable")).toBe(true)
|
|
});
|
|
|
|
it("returns false if a feature is at its quota", () => {
|
|
expect(FeatureUsageStore.isUsable("not-usable")).toBe(false)
|
|
});
|
|
|
|
it("warns if asking for an unsupported feature", () => {
|
|
spyOn(NylasEnv, "reportError")
|
|
expect(FeatureUsageStore.isUsable("unsupported")).toBe(false)
|
|
expect(NylasEnv.reportError).toHaveBeenCalled()
|
|
});
|
|
});
|
|
|
|
describe("useFeature", () => {
|
|
beforeEach(() => {
|
|
spyOn(SendFeatureUsageEventTask.prototype, "performRemote").andReturn(Promise.resolve(Task.Status.Success));
|
|
spyOn(IdentityStore, "saveIdentity").andCallFake((ident) => {
|
|
IdentityStore._identity = ident
|
|
})
|
|
spyOn(TaskQueueStatusStore, "waitForPerformLocal").andReturn(Promise.resolve())
|
|
});
|
|
|
|
it("returns the num remaining if successful", async () => {
|
|
let numLeft = await FeatureUsageStore.useFeature('is-usable');
|
|
expect(numLeft).toBe(1)
|
|
numLeft = await FeatureUsageStore.useFeature('is-usable');
|
|
expect(numLeft).toBe(0)
|
|
});
|
|
|
|
it("throws if it was over quota", async () => {
|
|
try {
|
|
await FeatureUsageStore.useFeature("not-usable");
|
|
throw new Error("This should throw")
|
|
} catch (err) {
|
|
expect(err.message).toMatch(/not usable/)
|
|
}
|
|
});
|
|
|
|
it("throws if using an unsupported feature", async () => {
|
|
spyOn(NylasEnv, "reportError")
|
|
try {
|
|
await FeatureUsageStore.useFeature("unsupported");
|
|
throw new Error("This should throw")
|
|
} catch (err) {
|
|
expect(err.message).toMatch(/supported/)
|
|
}
|
|
});
|
|
});
|
|
});
|