Merge pull request #2112 from automatisch/test-subscription

test: Implement subscription model tests
This commit is contained in:
Ömer Faruk Aydın
2024-10-02 13:53:46 +03:00
committed by GitHub
2 changed files with 171 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`Subscription model > jsonSchema should have correct validations 1`] = `
{
"properties": {
"cancelUrl": {
"type": "string",
},
"cancellationEffectiveDate": {
"type": "string",
},
"createdAt": {
"type": "string",
},
"deletedAt": {
"type": "string",
},
"id": {
"format": "uuid",
"type": "string",
},
"lastBillDate": {
"type": "string",
},
"nextBillAmount": {
"type": "string",
},
"nextBillDate": {
"type": "string",
},
"paddlePlanId": {
"type": "string",
},
"paddleSubscriptionId": {
"type": "string",
},
"status": {
"type": "string",
},
"updateUrl": {
"type": "string",
},
"updatedAt": {
"type": "string",
},
"userId": {
"format": "uuid",
"type": "string",
},
},
"required": [
"userId",
"paddleSubscriptionId",
"paddlePlanId",
"updateUrl",
"cancelUrl",
"status",
"nextBillAmount",
"nextBillDate",
],
"type": "object",
}
`;

View File

@@ -0,0 +1,108 @@
import { vi, describe, it, expect } from 'vitest';
import { DateTime } from 'luxon';
import Subscription from './subscription.ee';
import User from './user';
import UsageData from './usage-data.ee';
import Base from './base';
import { createSubscription } from '../../test/factories/subscription';
describe('Subscription model', () => {
it('tableName should return correct name', () => {
expect(Subscription.tableName).toBe('subscriptions');
});
it('jsonSchema should have correct validations', () => {
expect(Subscription.jsonSchema).toMatchSnapshot();
});
it('relationMappings should return correct associations', () => {
const relationMappings = Subscription.relationMappings();
const expectedRelations = {
user: {
relation: Base.BelongsToOneRelation,
modelClass: User,
join: {
from: 'subscription.user_id',
to: 'users.id',
},
},
usageData: {
relation: Base.HasManyRelation,
modelClass: UsageData,
join: {
from: 'subscriptions.id',
to: 'usage_data.subscription_id',
},
},
currentUsageData: {
relation: Base.HasOneRelation,
modelClass: UsageData,
join: {
from: 'subscriptions.id',
to: 'usage_data.subscription_id',
},
},
};
expect(relationMappings).toStrictEqual(expectedRelations);
});
it('plan should return paddle plan data', async () => {
const subscription = await createSubscription({
paddlePlanId: '47384',
});
const expectedPaddlePlan = {
limit: '10,000',
name: '10k - monthly',
price: '€20',
productId: '47384',
quota: 10000,
};
expect(subscription.plan).toStrictEqual(expectedPaddlePlan);
});
it('isCancelledAndValid should return true if deleted but cancellation effective date has not passed', async () => {
const subscription = await createSubscription({
status: 'deleted',
cancellationEffectiveDate: DateTime.now().plus({ days: 2 }).toString(),
});
expect(subscription.isCancelledAndValid).toBe(true);
});
describe('isValid', () => {
it('should return true if status is active', async () => {
const subscription = await createSubscription({
status: 'active',
});
expect(subscription.isValid).toBe(true);
});
it('should return true if status is past due', async () => {
const subscription = await createSubscription({
status: 'past_due',
});
expect(subscription.isValid).toBe(true);
});
it('should return true if subscription is cancelled and valid', async () => {
const subscription = await createSubscription();
vi.spyOn(subscription, 'isCancelledAndValid').mockReturnValue(false);
expect(subscription.isValid).toBe(true);
});
it('should return false if any condition is matched', async () => {
const subscription = await createSubscription({
status: 'not_valid',
});
expect(subscription.isValid).toBe(false);
});
});
});