1 import type { PlanIDs } from '@proton/payments';
2 import { CYCLE } from '@proton/shared/lib/constants';
3 import { getPlanIDs } from '@proton/shared/lib/helpers/subscription';
4 import { getSubscriptionMock } from '@proton/testing/data';
6 import { isSubscriptionUnchanged } from './helper';
8 describe('isSubscriptionUnchanged', () => {
9 it('returns true when subscription and planIds are deeply equal', () => {
10 const subscription = getSubscriptionMock();
11 const planIds: PlanIDs = getPlanIDs(subscription); // Assuming getPlanIDs is a function that extracts plan IDs from a subscription
13 const result = isSubscriptionUnchanged(subscription, planIds);
14 expect(result).toBe(true);
17 it('returns false when subscription and planIds are not deeply equal', () => {
18 const subscription = getSubscriptionMock();
19 const planIds: PlanIDs = {
23 const result = isSubscriptionUnchanged(subscription, planIds);
24 expect(result).toBe(false);
27 it('returns true when both subscription and planIds are empty', () => {
28 const result = isSubscriptionUnchanged(null, {});
29 expect(result).toBe(true);
32 it('returns false when subscription is null and planIds is not null', () => {
33 const planIds: PlanIDs = {
37 const result = isSubscriptionUnchanged(null, planIds);
38 expect(result).toBe(false);
41 it('returns false when subscription is not null and planIds is null', () => {
42 const subscription = getSubscriptionMock();
44 const result = isSubscriptionUnchanged(subscription, null as any);
45 expect(result).toBe(false);
48 it('should return true when cycle is the same as in the subscription', () => {
49 const subscription = getSubscriptionMock();
50 subscription.Cycle = CYCLE.MONTHLY;
52 const planIds: PlanIDs = getPlanIDs(subscription);
54 const result = isSubscriptionUnchanged(subscription, planIds, CYCLE.MONTHLY);
55 expect(result).toBe(true);
58 it('should return false when cycle is different from the subscription', () => {
59 const subscription = getSubscriptionMock();
60 subscription.Cycle = CYCLE.MONTHLY;
62 const planIds: PlanIDs = getPlanIDs(subscription);
64 const result = isSubscriptionUnchanged(subscription, planIds, CYCLE.YEARLY);
65 expect(result).toBe(false);
68 it('should return true if the upcoming subscription unchanged', () => {
69 const subscription = getSubscriptionMock();
70 subscription.Cycle = CYCLE.MONTHLY;
71 subscription.UpcomingSubscription = getSubscriptionMock();
72 subscription.UpcomingSubscription.Cycle = CYCLE.YEARLY;
74 const planIds: PlanIDs = getPlanIDs(subscription);
76 const currentSubscriptionUnchanged = isSubscriptionUnchanged(subscription, planIds, CYCLE.MONTHLY);
77 expect(currentSubscriptionUnchanged).toBe(true);
79 const upcomingSubscriptionUnchanged = isSubscriptionUnchanged(subscription, planIds, CYCLE.YEARLY);
80 expect(upcomingSubscriptionUnchanged).toBe(true);
83 it('should return false if there is no upcoming subscription', () => {
84 const subscription = getSubscriptionMock();
85 subscription.Cycle = CYCLE.MONTHLY;
87 const planIds: PlanIDs = getPlanIDs(subscription);
89 const currentSubscriptionUnchanged = isSubscriptionUnchanged(subscription, planIds, CYCLE.MONTHLY);
90 expect(currentSubscriptionUnchanged).toBe(true);
92 const upcomingSubscriptionUnchangedYearly = isSubscriptionUnchanged(subscription, planIds, CYCLE.YEARLY);
93 expect(upcomingSubscriptionUnchangedYearly).toBe(false);
95 const upcomingSubscriptionUnchangedTwoYears = isSubscriptionUnchanged(subscription, planIds, CYCLE.TWO_YEARS);
96 expect(upcomingSubscriptionUnchangedTwoYears).toBe(false);