From c2976080f631306603ae673c66d892f7a7124e85 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Wed, 2 Oct 2024 13:44:21 +0300 Subject: [PATCH] test: Implement subscription model tests --- .../subscription.ee.test.js.snap | 63 ++++++++++ .../src/models/subscription.ee.test.js | 108 ++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 packages/backend/src/models/__snapshots__/subscription.ee.test.js.snap create mode 100644 packages/backend/src/models/subscription.ee.test.js diff --git a/packages/backend/src/models/__snapshots__/subscription.ee.test.js.snap b/packages/backend/src/models/__snapshots__/subscription.ee.test.js.snap new file mode 100644 index 00000000..49a64a99 --- /dev/null +++ b/packages/backend/src/models/__snapshots__/subscription.ee.test.js.snap @@ -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", +} +`; diff --git a/packages/backend/src/models/subscription.ee.test.js b/packages/backend/src/models/subscription.ee.test.js new file mode 100644 index 00000000..35f24fa9 --- /dev/null +++ b/packages/backend/src/models/subscription.ee.test.js @@ -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); + }); + }); +});