diff --git a/src/stores/Howto/howto.store.test.ts b/src/stores/Howto/howto.store.test.ts index 764bc8d10f..a30f4f02fb 100644 --- a/src/stores/Howto/howto.store.test.ts +++ b/src/stores/Howto/howto.store.test.ts @@ -50,6 +50,9 @@ async function factory(howtoOverloads: Partial = {}) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore setFn: store.db.set, + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + getFn: store.db.get, } } @@ -403,4 +406,39 @@ describe('howto.store', () => { expect(store.activeHowto).toBe(null) }) }) + + describe('incrementDownloadCount', () => { + it('increments download count by one', async () => { + const { store, howToItem, setFn } = await factory() + + const downloads = howToItem.total_downloads! + // Act + const updatedDownloads = await store.incrementDownloadCount(howToItem._id) + + expect(setFn).toBeCalledTimes(1) + expect(updatedDownloads).toBe(downloads + 1) + }) + }) + + describe('incrementViews', () => { + it('data fetched from server db', async () => { + const { store, howToItem, getFn } = await factory() + + // Act + await store.incrementViewCount(howToItem._id) + + expect(getFn).toBeCalledTimes(1) + expect(getFn).toHaveBeenCalledWith('server') + }) + it('increments views by one', async () => { + const { store, howToItem, setFn } = await factory() + + const views = howToItem.total_views! + // Act + const updatedViews = await store.incrementViewCount(howToItem._id) + + expect(setFn).toHaveBeenCalledTimes(1) + expect(updatedViews).toBe(views + 1) + }) + }) }) diff --git a/src/stores/Howto/howto.store.tsx b/src/stores/Howto/howto.store.tsx index 1828d7632c..44bee11d56 100644 --- a/src/stores/Howto/howto.store.tsx +++ b/src/stores/Howto/howto.store.tsx @@ -181,7 +181,7 @@ export class HowtoStore extends ModuleStore { public async incrementDownloadCount(howToID: string) { const dbRef = this.db.collection(COLLECTION_NAME).doc(howToID) - const howToData = await toJS(dbRef.get()) + const howToData = await toJS(dbRef.get('server')) const totalDownloads = howToData?.total_downloads || 0 if (howToData) { @@ -203,7 +203,7 @@ export class HowtoStore extends ModuleStore { public async incrementViewCount(howToID: string) { const dbRef = this.db.collection(COLLECTION_NAME).doc(howToID) - const howToData = await toJS(dbRef.get()) + const howToData = await toJS(dbRef.get('server')) const totalViews = howToData?.total_views || 0 if (howToData) { diff --git a/src/stores/Research/research.store.test.ts b/src/stores/Research/research.store.test.ts index 000a9c5fbb..ccd08b4f0a 100644 --- a/src/stores/Research/research.store.test.ts +++ b/src/stores/Research/research.store.test.ts @@ -62,6 +62,9 @@ const factory = async (researchItemOverloads: any = {}) => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore setFn: store.db.set, + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + getFn: store.db.get, } } @@ -501,4 +504,27 @@ describe('research.store', () => { }) }) }) + describe('incrementViews', () => { + it('data fetched from server db', async () => { + const { store, researchItem, getFn } = await factory() + // necessary as getFn gets called when active research item is set in factory + jest.clearAllMocks() + + // Act + await store.incrementViewCount(researchItem._id) + + expect(getFn).toBeCalledTimes(1) + expect(getFn).toHaveBeenCalledWith('server') + }) + it('increments views by one', async () => { + const { store, researchItem, setFn } = await factory() + + const views = researchItem.total_views! + // Act + const updatedViews = await store.incrementViewCount(researchItem._id) + + expect(setFn).toHaveBeenCalledTimes(1) + expect(updatedViews).toBe(views + 1) + }) + }) }) diff --git a/src/stores/Research/research.store.tsx b/src/stores/Research/research.store.tsx index 9df3be91ac..040d280712 100644 --- a/src/stores/Research/research.store.tsx +++ b/src/stores/Research/research.store.tsx @@ -148,7 +148,7 @@ export class ResearchStore extends ModuleStore { public async incrementViewCount(id: string) { const dbRef = this.db.collection(COLLECTION_NAME).doc(id) - const researchData = await toJS(dbRef.get()) + const researchData = await toJS(dbRef.get('server')) const totalViews = researchData?.total_views || 0 if (researchData) { diff --git a/src/test/factories/Howto.ts b/src/test/factories/Howto.ts index d13aa0409a..02057ec5ea 100644 --- a/src/test/factories/Howto.ts +++ b/src/test/factories/Howto.ts @@ -38,6 +38,8 @@ export const FactoryHowto = ( updated: '', size: 0, }, + total_views: faker.datatype.number(), + total_downloads: faker.datatype.number(), ...howtoOverloads, }) diff --git a/src/test/factories/ResearchItem.ts b/src/test/factories/ResearchItem.ts index 5dad69149d..9e5dee4085 100644 --- a/src/test/factories/ResearchItem.ts +++ b/src/test/factories/ResearchItem.ts @@ -34,5 +34,6 @@ export const FactoryResearchItem = ( 'accepted', ]), mentions: [], + total_views: faker.datatype.number(), ...researchItemOverloads, })