|
| 1 | +import { lobbysideCustomFields } from 'codecrafters-frontend/components/lobbyside-widget'; |
| 2 | +import { module, test } from 'qunit'; |
| 3 | +import type UserModel from 'codecrafters-frontend/models/user'; |
| 4 | + |
| 5 | +module('Unit | Component | lobbyside-widget', function () { |
| 6 | + function createUser(overrides: Partial<UserModel> = {}): UserModel { |
| 7 | + return { |
| 8 | + createdAt: new Date('2023-04-15T10:30:00.000Z'), |
| 9 | + isVip: false, |
| 10 | + activeSubscription: null, |
| 11 | + hasActiveSubscription: false, |
| 12 | + teamHasActiveSubscription: false, |
| 13 | + courseParticipations: [], |
| 14 | + ...overrides, |
| 15 | + } as unknown as UserModel; |
| 16 | + } |
| 17 | + |
| 18 | + test('returns empty fields for a missing user', function (assert) { |
| 19 | + assert.deepEqual(lobbysideCustomFields(null), { |
| 20 | + stages_completed: '0', |
| 21 | + signed_up_date: '', |
| 22 | + subscription_type: '', |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + test('formats signed_up_date as an ISO calendar date', function (assert) { |
| 27 | + const user = createUser({ createdAt: new Date('2023-04-15T10:30:00.000Z') }); |
| 28 | + |
| 29 | + assert.strictEqual(lobbysideCustomFields(user).signed_up_date, '2023-04-15'); |
| 30 | + }); |
| 31 | + |
| 32 | + test('sums completed stage slugs across all course participations', function (assert) { |
| 33 | + const user = createUser({ |
| 34 | + courseParticipations: [{ completedStageSlugs: ['s1', 's2', 's3'] }, { completedStageSlugs: ['s1'] }, { completedStageSlugs: [] }], |
| 35 | + } as unknown as Partial<UserModel>); |
| 36 | + |
| 37 | + assert.strictEqual(lobbysideCustomFields(user).stages_completed, '4'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('subscription_type prefers lifetime over every other signal', function (assert) { |
| 41 | + const user = createUser({ |
| 42 | + isVip: true, |
| 43 | + hasActiveSubscription: true, |
| 44 | + teamHasActiveSubscription: true, |
| 45 | + activeSubscription: { isLifetimeMembership: true } as UserModel['activeSubscription'], |
| 46 | + }); |
| 47 | + |
| 48 | + assert.strictEqual(lobbysideCustomFields(user).subscription_type, 'lifetime'); |
| 49 | + }); |
| 50 | + |
| 51 | + test('subscription_type falls back through individual, team, vip, then free', function (assert) { |
| 52 | + assert.strictEqual(lobbysideCustomFields(createUser({ hasActiveSubscription: true })).subscription_type, 'individual'); |
| 53 | + |
| 54 | + assert.strictEqual(lobbysideCustomFields(createUser({ teamHasActiveSubscription: true })).subscription_type, 'team'); |
| 55 | + |
| 56 | + assert.strictEqual(lobbysideCustomFields(createUser({ isVip: true })).subscription_type, 'vip'); |
| 57 | + |
| 58 | + assert.strictEqual(lobbysideCustomFields(createUser()).subscription_type, 'free'); |
| 59 | + }); |
| 60 | +}); |
0 commit comments