|
7 | 7 | addHours, |
8 | 8 | addSeconds, |
9 | 9 | addYears, |
| 10 | + format, |
10 | 11 | startOfDay, |
11 | 12 | startOfISOWeek, |
12 | 13 | subDays, |
@@ -59,6 +60,7 @@ import { |
59 | 60 | } from '../src/entity'; |
60 | 61 | import { UserProfileAnalytics } from '../src/entity/user/UserProfileAnalytics'; |
61 | 62 | import { UserProfileAnalyticsHistory } from '../src/entity/user/UserProfileAnalyticsHistory'; |
| 63 | +import { PostAnalyticsHistory } from '../src/entity/posts/PostAnalyticsHistory'; |
62 | 64 | import { sourcesFixture } from './fixture/source'; |
63 | 65 | import { |
64 | 66 | CioTransactionalMessageTemplateId, |
@@ -7963,3 +7965,87 @@ describe('query userProfileAnalyticsHistory', () => { |
7963 | 7965 | expect(res.data.userProfileAnalyticsHistory.edges).toHaveLength(0); |
7964 | 7966 | }); |
7965 | 7967 | }); |
| 7968 | + |
| 7969 | +describe('query userPostsAnalytics', () => { |
| 7970 | + const QUERY = /* GraphQL */ ` |
| 7971 | + query UserPostsAnalytics { |
| 7972 | + userPostsAnalytics { |
| 7973 | + id |
| 7974 | + impressions |
| 7975 | + upvotes |
| 7976 | + comments |
| 7977 | + } |
| 7978 | + } |
| 7979 | + `; |
| 7980 | + |
| 7981 | + it('should require authentication', async () => { |
| 7982 | + await testQueryErrorCode(client, { query: QUERY }, 'UNAUTHENTICATED'); |
| 7983 | + }); |
| 7984 | + |
| 7985 | + it('should return null when no analytics exist', async () => { |
| 7986 | + loggedUser = '1'; |
| 7987 | + |
| 7988 | + const res = await client.query(QUERY); |
| 7989 | + |
| 7990 | + expect(res.errors).toBeFalsy(); |
| 7991 | + expect(res.data.userPostsAnalytics).toBeNull(); |
| 7992 | + }); |
| 7993 | +}); |
| 7994 | + |
| 7995 | +describe('query userPostsAnalyticsHistory', () => { |
| 7996 | + const QUERY = /* GraphQL */ ` |
| 7997 | + query UserPostsAnalyticsHistory { |
| 7998 | + userPostsAnalyticsHistory { |
| 7999 | + date |
| 8000 | + impressions |
| 8001 | + impressionsAds |
| 8002 | + } |
| 8003 | + } |
| 8004 | + `; |
| 8005 | + |
| 8006 | + beforeEach(async () => { |
| 8007 | + await saveFixtures(con, Post, [ |
| 8008 | + { |
| 8009 | + id: 'p1-upah', |
| 8010 | + shortId: 'sp1-upah', |
| 8011 | + title: 'Test Post', |
| 8012 | + url: 'https://example.com/p1-upah', |
| 8013 | + sourceId: 'a', |
| 8014 | + authorId: '1', |
| 8015 | + }, |
| 8016 | + ]); |
| 8017 | + |
| 8018 | + await con.getRepository(PostAnalyticsHistory).save([ |
| 8019 | + { |
| 8020 | + id: 'p1-upah', |
| 8021 | + date: format(new Date(), 'yyyy-MM-dd'), |
| 8022 | + impressions: 100, |
| 8023 | + impressionsAds: 50, |
| 8024 | + }, |
| 8025 | + { |
| 8026 | + id: 'p1-upah', |
| 8027 | + date: format(subDays(new Date(), 1), 'yyyy-MM-dd'), |
| 8028 | + impressions: 80, |
| 8029 | + impressionsAds: 40, |
| 8030 | + }, |
| 8031 | + ]); |
| 8032 | + }); |
| 8033 | + |
| 8034 | + it('should require authentication', async () => { |
| 8035 | + await testQueryErrorCode(client, { query: QUERY }, 'UNAUTHENTICATED'); |
| 8036 | + }); |
| 8037 | + |
| 8038 | + it('should return aggregated daily impressions history', async () => { |
| 8039 | + loggedUser = '1'; |
| 8040 | + |
| 8041 | + const res = await client.query(QUERY); |
| 8042 | + |
| 8043 | + expect(res.errors).toBeFalsy(); |
| 8044 | + expect(res.data.userPostsAnalyticsHistory).toHaveLength(2); |
| 8045 | + expect(res.data.userPostsAnalyticsHistory[0]).toMatchObject({ |
| 8046 | + date: expect.any(String), |
| 8047 | + impressions: 150, |
| 8048 | + impressionsAds: 50, |
| 8049 | + }); |
| 8050 | + }); |
| 8051 | +}); |
0 commit comments