Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/stores/Howto/howto.store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ async function factory(howtoOverloads: Partial<IHowtoDB> = {}) {
// 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,
}
}

Expand Down Expand Up @@ -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)
})
})
})
4 changes: 2 additions & 2 deletions src/stores/Howto/howto.store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class HowtoStore extends ModuleStore {

public async incrementDownloadCount(howToID: string) {
const dbRef = this.db.collection<IHowto>(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) {
Expand All @@ -203,7 +203,7 @@ export class HowtoStore extends ModuleStore {

public async incrementViewCount(howToID: string) {
const dbRef = this.db.collection<IHowto>(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) {
Expand Down
26 changes: 26 additions & 0 deletions src/stores/Research/research.store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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)
})
})
})
2 changes: 1 addition & 1 deletion src/stores/Research/research.store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class ResearchStore extends ModuleStore {

public async incrementViewCount(id: string) {
const dbRef = this.db.collection<IResearchDB>(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) {
Expand Down
2 changes: 2 additions & 0 deletions src/test/factories/Howto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export const FactoryHowto = (
updated: '',
size: 0,
},
total_views: faker.datatype.number(),
total_downloads: faker.datatype.number(),
...howtoOverloads,
})

Expand Down
1 change: 1 addition & 0 deletions src/test/factories/ResearchItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ export const FactoryResearchItem = (
'accepted',
]),
mentions: [],
total_views: faker.datatype.number(),
...researchItemOverloads,
})