From aed7e2694e32d3f8cb991a6a79a841111921dd6e Mon Sep 17 00:00:00 2001 From: Nicolas MASSART Date: Fri, 2 Aug 2024 18:04:47 +0200 Subject: [PATCH] update unit test to match expectation Test do not pass yet on purpose We have to validate the test use case logic and then update implementation --- app/core/Analytics/MetaMetrics.test.ts | 154 ++++++++++++++++++------- 1 file changed, 111 insertions(+), 43 deletions(-) diff --git a/app/core/Analytics/MetaMetrics.test.ts b/app/core/Analytics/MetaMetrics.test.ts index 0420b3837f2..6ecf02ff416 100644 --- a/app/core/Analytics/MetaMetrics.test.ts +++ b/app/core/Analytics/MetaMetrics.test.ts @@ -161,7 +161,7 @@ describe('MetaMetrics', () => { }); }); - it('does not track event when diabled', async () => { + it('does not track event when disabled', async () => { const metaMetrics = TestMetaMetrics.getInstance(); expect(await metaMetrics.configure()).toBeTruthy(); const event: IMetaMetricsEvent = { category: 'event1' }; @@ -176,80 +176,148 @@ describe('MetaMetrics', () => { expect(segmentMockClient.track).not.toHaveBeenCalled(); }); - it('tracks anonymous event', async () => { + it('tracks event without updating dataRecorded status', async () => { const metaMetrics = TestMetaMetrics.getInstance(); + expect(await metaMetrics.configure()).toBeTruthy(); await metaMetrics.enable(); const event: IMetaMetricsEvent = { category: 'event1' }; const properties = { prop1: 'value1' }; - metaMetrics.trackAnonymousEvent(event, properties); + metaMetrics.trackEvent(event, properties, false); + expect(StorageWrapper.getItem).toHaveBeenCalledWith(METRICS_OPT_IN); // TODO: Replace "any" with type // eslint-disable-next-line @typescript-eslint/no-explicit-any const { segmentMockClient } = global as any; - // the anonymous part should not have a user id expect(segmentMockClient.track).toHaveBeenCalledWith(event.category, { - anonymous: true, + anonymous: false, ...properties, }); - // non anonymous part should not have properties - expect(segmentMockClient.track).toHaveBeenCalledWith(event.category, { - anonymous: true, - }); + expect(metaMetrics.isDataRecorded()).toBeFalsy(); }); - it('tracks anonymous event without param', async () => { + it('tracks non-anonymous event when some props are anonymous', async () => { const metaMetrics = TestMetaMetrics.getInstance(); await metaMetrics.enable(); const event: IMetaMetricsEvent = { category: 'event1' }; + const nonAnonProp = { non_anon_prop: 'value1' }; + const someAnonProps = { + anon_prop: { anonymous: true, value: 'anonValue2' }, + }; + const properties = { ...nonAnonProp, ...someAnonProps }; - metaMetrics.trackAnonymousEvent(event); + metaMetrics.trackEvent(event, properties); // TODO: Replace "any" with type // eslint-disable-next-line @typescript-eslint/no-explicit-any const { segmentMockClient } = global as any; - // the anonymous part should not have a user id - expect(segmentMockClient.track).toHaveBeenCalledWith(event.category, { - anonymous: true, - ...{}, - }); - // non anonymous part should not have properties + + // Only one non-anonymous event should be tracked + expect(segmentMockClient.track).toHaveBeenCalledTimes(1); + + // When tracking is not anonymous, event should have both non-anonymous and anonymous properties. + // This makes no sense and should be fixed in the code as I believe no one should send anonymous properties + // in a non-anonymous event. But in case it happens, we should track it this way. expect(segmentMockClient.track).toHaveBeenCalledWith(event.category, { - anonymous: true, + anonymous: false, + ...someAnonProps, + ...nonAnonProp, }); }); - it('does not track anonymous event if disabled', async () => { - const metaMetrics = TestMetaMetrics.getInstance(); - const event: IMetaMetricsEvent = { category: 'event1' }; - const properties = { prop1: 'value1' }; + describe('anonymous event', () => { + it('tracks two events when all props are non anonymous', async () => { + const metaMetrics = TestMetaMetrics.getInstance(); + await metaMetrics.enable(); + const event: IMetaMetricsEvent = { category: 'event1' }; + const properties = { prop1: 'value1' }; - metaMetrics.trackAnonymousEvent(event, properties); + metaMetrics.trackAnonymousEvent(event, properties); - // TODO: Replace "any" with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const { segmentMockClient } = global as any; - expect(segmentMockClient.track).not.toHaveBeenCalled(); - }); + // TODO: Replace "any" with type + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const { segmentMockClient } = global as any; - it('tracks event without updating dataRecorded status', async () => { - const metaMetrics = TestMetaMetrics.getInstance(); - expect(await metaMetrics.configure()).toBeTruthy(); - await metaMetrics.enable(); - const event: IMetaMetricsEvent = { category: 'event1' }; - const properties = { prop1: 'value1' }; + // anonymous event has all the properties + expect(segmentMockClient.track).toHaveBeenCalledWith(event.category, { + anonymous: true, + ...properties, + }); - metaMetrics.trackEvent(event, properties, false); + // non-anonymous event has no properties + expect(segmentMockClient.track).toHaveBeenCalledWith(event.category, { + anonymous: false, + }); + }); - expect(StorageWrapper.getItem).toHaveBeenCalledWith(METRICS_OPT_IN); - // TODO: Replace "any" with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const { segmentMockClient } = global as any; - expect(segmentMockClient.track).toHaveBeenCalledWith(event.category, { - anonymous: false, - ...properties, + it('tracks two empty events when no props', async () => { + const metaMetrics = TestMetaMetrics.getInstance(); + await metaMetrics.enable(); + const event: IMetaMetricsEvent = { category: 'event1' }; + + metaMetrics.trackAnonymousEvent(event); + + // TODO: Replace "any" with type + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const { segmentMockClient } = global as any; + + // anonymous event has no properties + expect(segmentMockClient.track).toHaveBeenCalledWith(event.category, { + anonymous: true, + }); + + // non-anonymous event has no properties + expect(segmentMockClient.track).toHaveBeenCalledWith(event.category, { + anonymous: false, + }); + }); + + it('tracks two events when some props are anonymous', async () => { + const metaMetrics = TestMetaMetrics.getInstance(); + await metaMetrics.enable(); + const event: IMetaMetricsEvent = { category: 'event1' }; + const nonAnonProp = { non_anon_prop: 'value1' }; + const anonProp = { + anon_prop: { anonymous: true, value: 'anonValue2' }, + }; + const properties = { ...nonAnonProp, ...anonProp }; + + metaMetrics.trackAnonymousEvent(event, properties); + + // TODO: Replace "any" with type + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const { segmentMockClient } = global as any; + + // non-anonymous event only has the non-anonymous properties. + expect(segmentMockClient.track).toHaveBeenCalledWith(event.category, { + anonymous: false, + ...nonAnonProp, + }); + + // anonymous event has all properties + expect(segmentMockClient.track).toHaveBeenCalledWith(event.category, { + anonymous: true, + ...anonProp, + ...nonAnonProp, + }); + + // Only two events should be tracked, one anonymous and one non-anonymous + expect(segmentMockClient.track).toHaveBeenCalledTimes(2); + }); + + it('does not track any event if disabled', async () => { + const metaMetrics = TestMetaMetrics.getInstance(); + const event: IMetaMetricsEvent = { category: 'event1' }; + const properties = { prop1: 'value1' }; + + metaMetrics.trackAnonymousEvent(event, properties); + + // TODO: Replace "any" with type + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const { segmentMockClient } = global as any; + + expect(segmentMockClient.track).not.toHaveBeenCalled(); }); - expect(metaMetrics.isDataRecorded()).toBeFalsy(); }); describe('Legacy events', () => {