|
| 1 | +/** |
| 2 | + * Module dependencies. |
| 3 | + */ |
| 4 | +import schema from '../models/subscription.schema.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Unit tests |
| 8 | + */ |
| 9 | +describe('Billing unit tests:', () => { |
| 10 | + describe('Subscription schema', () => { |
| 11 | + let subscription; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + subscription = { |
| 15 | + organization: '507f1f77bcf86cd799439011', |
| 16 | + plan: 'free', |
| 17 | + status: 'active', |
| 18 | + }; |
| 19 | + }); |
| 20 | + |
| 21 | + test('should be valid a subscription example without problems', (done) => { |
| 22 | + const result = schema.Subscription.safeParse(subscription); |
| 23 | + expect(typeof result).toBe('object'); |
| 24 | + expect(result.error).toBeFalsy(); |
| 25 | + done(); |
| 26 | + }); |
| 27 | + |
| 28 | + test('should be able to show an error when trying a schema without organization', (done) => { |
| 29 | + subscription.organization = ''; |
| 30 | + |
| 31 | + const result = schema.Subscription.safeParse(subscription); |
| 32 | + expect(typeof result).toBe('object'); |
| 33 | + expect(result.error).toBeDefined(); |
| 34 | + done(); |
| 35 | + }); |
| 36 | + |
| 37 | + test('should be valid with all optional fields', (done) => { |
| 38 | + subscription.stripeCustomerId = 'cus_123'; |
| 39 | + subscription.stripeSubscriptionId = 'sub_456'; |
| 40 | + subscription.currentPeriodEnd = '2026-12-31T00:00:00.000Z'; |
| 41 | + subscription.cancelAtPeriodEnd = true; |
| 42 | + |
| 43 | + const result = schema.Subscription.safeParse(subscription); |
| 44 | + expect(typeof result).toBe('object'); |
| 45 | + expect(result.error).toBeFalsy(); |
| 46 | + expect(result.data.cancelAtPeriodEnd).toBe(true); |
| 47 | + done(); |
| 48 | + }); |
| 49 | + |
| 50 | + test('should default plan to free', (done) => { |
| 51 | + delete subscription.plan; |
| 52 | + |
| 53 | + const result = schema.Subscription.safeParse(subscription); |
| 54 | + expect(result.error).toBeFalsy(); |
| 55 | + expect(result.data.plan).toBe('free'); |
| 56 | + done(); |
| 57 | + }); |
| 58 | + |
| 59 | + test('should default status to active', (done) => { |
| 60 | + delete subscription.status; |
| 61 | + |
| 62 | + const result = schema.Subscription.safeParse(subscription); |
| 63 | + expect(result.error).toBeFalsy(); |
| 64 | + expect(result.data.status).toBe('active'); |
| 65 | + done(); |
| 66 | + }); |
| 67 | + |
| 68 | + test('should reject invalid plan value', (done) => { |
| 69 | + subscription.plan = 'invalid'; |
| 70 | + |
| 71 | + const result = schema.Subscription.safeParse(subscription); |
| 72 | + expect(result.error).toBeDefined(); |
| 73 | + done(); |
| 74 | + }); |
| 75 | + |
| 76 | + test('should reject invalid status value', (done) => { |
| 77 | + subscription.status = 'invalid'; |
| 78 | + |
| 79 | + const result = schema.Subscription.safeParse(subscription); |
| 80 | + expect(result.error).toBeDefined(); |
| 81 | + done(); |
| 82 | + }); |
| 83 | + |
| 84 | + test('should accept all valid plan values', (done) => { |
| 85 | + for (const plan of ['free', 'starter', 'pro']) { |
| 86 | + subscription.plan = plan; |
| 87 | + const result = schema.Subscription.safeParse(subscription); |
| 88 | + expect(result.error).toBeFalsy(); |
| 89 | + } |
| 90 | + done(); |
| 91 | + }); |
| 92 | + |
| 93 | + test('should accept all valid status values', (done) => { |
| 94 | + for (const status of ['active', 'past_due', 'canceled', 'trialing', 'incomplete']) { |
| 95 | + subscription.status = status; |
| 96 | + const result = schema.Subscription.safeParse(subscription); |
| 97 | + expect(result.error).toBeFalsy(); |
| 98 | + } |
| 99 | + done(); |
| 100 | + }); |
| 101 | + |
| 102 | + test('should default cancelAtPeriodEnd to false', (done) => { |
| 103 | + const result = schema.Subscription.safeParse(subscription); |
| 104 | + expect(result.error).toBeFalsy(); |
| 105 | + expect(result.data.cancelAtPeriodEnd).toBe(false); |
| 106 | + done(); |
| 107 | + }); |
| 108 | + |
| 109 | + test('should strip unknown fields with SubscriptionUpdate', (done) => { |
| 110 | + const update = { plan: 'pro', unknown: 'field' }; |
| 111 | + const result = schema.SubscriptionUpdate.safeParse(update); |
| 112 | + expect(result.error).toBeFalsy(); |
| 113 | + expect(result.data?.unknown).toBeUndefined(); |
| 114 | + done(); |
| 115 | + }); |
| 116 | + |
| 117 | + test('should allow partial updates with SubscriptionUpdate', (done) => { |
| 118 | + const update = { plan: 'starter' }; |
| 119 | + const result = schema.SubscriptionUpdate.safeParse(update); |
| 120 | + expect(result.error).toBeFalsy(); |
| 121 | + expect(result.data.plan).toBe('starter'); |
| 122 | + done(); |
| 123 | + }); |
| 124 | + |
| 125 | + test('should reject invalid ObjectId for organization', (done) => { |
| 126 | + subscription.organization = 'not-an-objectid'; |
| 127 | + |
| 128 | + const result = schema.Subscription.safeParse(subscription); |
| 129 | + expect(result.error).toBeDefined(); |
| 130 | + done(); |
| 131 | + }); |
| 132 | + |
| 133 | + test('should accept valid ObjectId for organization', (done) => { |
| 134 | + subscription.organization = '507f1f77bcf86cd799439011'; |
| 135 | + |
| 136 | + const result = schema.Subscription.safeParse(subscription); |
| 137 | + expect(result.error).toBeFalsy(); |
| 138 | + done(); |
| 139 | + }); |
| 140 | + |
| 141 | + test('should normalize empty stripeCustomerId to undefined', (done) => { |
| 142 | + subscription.stripeCustomerId = ''; |
| 143 | + |
| 144 | + const result = schema.Subscription.safeParse(subscription); |
| 145 | + expect(result.error).toBeFalsy(); |
| 146 | + expect(result.data.stripeCustomerId).toBeUndefined(); |
| 147 | + done(); |
| 148 | + }); |
| 149 | + |
| 150 | + test('should normalize empty stripeSubscriptionId to undefined', (done) => { |
| 151 | + subscription.stripeSubscriptionId = ''; |
| 152 | + |
| 153 | + const result = schema.Subscription.safeParse(subscription); |
| 154 | + expect(result.error).toBeFalsy(); |
| 155 | + expect(result.data.stripeSubscriptionId).toBeUndefined(); |
| 156 | + done(); |
| 157 | + }); |
| 158 | + |
| 159 | + test('should preserve valid stripeCustomerId', (done) => { |
| 160 | + subscription.stripeCustomerId = 'cus_abc123'; |
| 161 | + |
| 162 | + const result = schema.Subscription.safeParse(subscription); |
| 163 | + expect(result.error).toBeFalsy(); |
| 164 | + expect(result.data.stripeCustomerId).toBe('cus_abc123'); |
| 165 | + done(); |
| 166 | + }); |
| 167 | + |
| 168 | + test('should preserve valid stripeSubscriptionId', (done) => { |
| 169 | + subscription.stripeSubscriptionId = 'sub_xyz789'; |
| 170 | + |
| 171 | + const result = schema.Subscription.safeParse(subscription); |
| 172 | + expect(result.error).toBeFalsy(); |
| 173 | + expect(result.data.stripeSubscriptionId).toBe('sub_xyz789'); |
| 174 | + done(); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments