|
| 1 | +import type { CreatorAchievement } from '@dailydotdev/shared/src/graphql/creatorAchievements'; |
| 2 | +import { CreatorAchievementType } from '@dailydotdev/shared/src/graphql/creatorAchievements'; |
| 3 | +import { |
| 4 | + achievementCategoryLabel, |
| 5 | + achievementPeriodLabel, |
| 6 | + achievementSubtitle, |
| 7 | + achievementTitle, |
| 8 | + describableAchievements, |
| 9 | +} from '../components/analytics/creator/achievements'; |
| 10 | + |
| 11 | +const achievement = ( |
| 12 | + partial: Partial<CreatorAchievement> = {}, |
| 13 | +): CreatorAchievement => ({ |
| 14 | + id: 'ca1', |
| 15 | + type: CreatorAchievementType.CategoryRanking, |
| 16 | + achievedAt: '2026-09-01T00:00:00.000Z', |
| 17 | + post: { |
| 18 | + id: 'p1', |
| 19 | + title: 'Shipping a design system', |
| 20 | + commentsPermalink: 'http://localhost:5002/posts/p1', |
| 21 | + }, |
| 22 | + keyword: { value: 'webdev', flags: { title: 'Web Development' } }, |
| 23 | + periodStart: '2026-08-01T00:00:00.000Z', |
| 24 | + periodEnd: '2026-09-01T00:00:00.000Z', |
| 25 | + threshold: null, |
| 26 | + rank: 2, |
| 27 | + measuredValue: null, |
| 28 | + evidenceUrl: 'https://daily.dev/tags/webdev/best-of/2026/08', |
| 29 | + isHistorical: false, |
| 30 | + ...partial, |
| 31 | +}); |
| 32 | + |
| 33 | +describe('achievementCategoryLabel', () => { |
| 34 | + it('should prefer the display title over the raw keyword', () => { |
| 35 | + expect(achievementCategoryLabel(achievement())).toEqual('Web Development'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should fall back to the keyword when it has no title', () => { |
| 39 | + expect( |
| 40 | + achievementCategoryLabel( |
| 41 | + achievement({ keyword: { value: 'rust', flags: null } }), |
| 42 | + ), |
| 43 | + ).toEqual('rust'); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe('achievementPeriodLabel', () => { |
| 48 | + it('should name the month the period starts in, in UTC', () => { |
| 49 | + expect(achievementPeriodLabel(achievement())).toEqual('August 2026'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should have nothing to say for a threshold award', () => { |
| 53 | + expect( |
| 54 | + achievementPeriodLabel(achievement({ periodStart: null })), |
| 55 | + ).toBeNull(); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('achievementTitle', () => { |
| 60 | + it('should state the placement and category for a ranking', () => { |
| 61 | + expect(achievementTitle(achievement())).toEqual('#2 in Web Development'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should state the threshold, not the measured value, for a milestone', () => { |
| 65 | + expect( |
| 66 | + achievementTitle( |
| 67 | + achievement({ |
| 68 | + type: CreatorAchievementType.PostUpvoteMilestone, |
| 69 | + rank: null, |
| 70 | + threshold: 100, |
| 71 | + measuredValue: 137, |
| 72 | + }), |
| 73 | + ), |
| 74 | + ).toEqual('100 upvotes'); |
| 75 | + expect( |
| 76 | + achievementTitle( |
| 77 | + achievement({ |
| 78 | + type: CreatorAchievementType.CreatorImpressionMilestone, |
| 79 | + rank: null, |
| 80 | + threshold: 100000, |
| 81 | + measuredValue: 104233, |
| 82 | + }), |
| 83 | + ), |
| 84 | + ).toEqual('100K impressions'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should say nothing for an award it cannot describe', () => { |
| 88 | + expect(achievementTitle(achievement({ rank: null }))).toBeNull(); |
| 89 | + expect( |
| 90 | + achievementTitle( |
| 91 | + achievement({ |
| 92 | + type: 'unknown_future_type' as CreatorAchievementType, |
| 93 | + }), |
| 94 | + ), |
| 95 | + ).toBeNull(); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('achievementSubtitle', () => { |
| 100 | + it('should caption a ranking with its month', () => { |
| 101 | + expect(achievementSubtitle(achievement())).toEqual('August 2026'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should caption an article milestone with the article', () => { |
| 105 | + expect( |
| 106 | + achievementSubtitle( |
| 107 | + achievement({ |
| 108 | + type: CreatorAchievementType.PostUpvoteMilestone, |
| 109 | + periodStart: null, |
| 110 | + }), |
| 111 | + ), |
| 112 | + ).toEqual('Shipping a design system'); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe('describableAchievements', () => { |
| 117 | + it('should keep every award it can describe', () => { |
| 118 | + const ranking = achievement(); |
| 119 | + const milestone = achievement({ |
| 120 | + id: 'ca3', |
| 121 | + type: CreatorAchievementType.CreatorImpressionMilestone, |
| 122 | + rank: null, |
| 123 | + threshold: 100000, |
| 124 | + post: null, |
| 125 | + keyword: null, |
| 126 | + periodStart: null, |
| 127 | + periodEnd: null, |
| 128 | + }); |
| 129 | + |
| 130 | + expect(describableAchievements([ranking, milestone])).toEqual([ |
| 131 | + ranking, |
| 132 | + milestone, |
| 133 | + ]); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should drop awards this build cannot describe', () => { |
| 137 | + const known = achievement(); |
| 138 | + const unknown = achievement({ |
| 139 | + id: 'ca2', |
| 140 | + type: 'unknown_future_type' as CreatorAchievementType, |
| 141 | + }); |
| 142 | + |
| 143 | + expect(describableAchievements([known, unknown])).toEqual([known]); |
| 144 | + }); |
| 145 | +}); |
0 commit comments