-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: display research draft updates for author
- Loading branch information
1 parent
0ae5bd2
commit 1d4379f
Showing
7 changed files
with
270 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import { ResearchUpdateStatus } from 'oa-shared' | ||
import { FactoryResearchItemUpdate } from 'src/test/factories/ResearchItem' | ||
|
||
import { | ||
getResearchTotalCommentCount, | ||
researchUpdateStatusFilter, | ||
} from './researchHelpers' | ||
|
||
import type { IResearch } from 'src/models' | ||
|
||
describe('Research Helpers', () => { | ||
describe('getResearchTotalCommentCount Function', () => { | ||
it('should return 0 when item has no updates', () => { | ||
const item = { item: {} } as any | ||
expect(getResearchTotalCommentCount(item)).toBe(0) | ||
}) | ||
|
||
it('should return 0 when updates have no comments', () => { | ||
const item = { | ||
updates: Array.from({ length: 3 }).fill( | ||
FactoryResearchItemUpdate({ | ||
status: ResearchUpdateStatus.PUBLISHED, | ||
_deleted: false, | ||
}), | ||
), | ||
} as IResearch.ItemDB | ||
expect(getResearchTotalCommentCount(item)).toBe(0) | ||
}) | ||
|
||
it('should use totalCommentCount if present', () => { | ||
const item = { | ||
totalCommentCount: 5, | ||
updates: Array.from({ length: 3 }).fill( | ||
FactoryResearchItemUpdate({ | ||
status: ResearchUpdateStatus.PUBLISHED, | ||
_deleted: false, | ||
}), | ||
), | ||
} as IResearch.ItemDB | ||
expect(getResearchTotalCommentCount(item)).toBe(5) | ||
}) | ||
|
||
it('should use totalCommentCount when 0', () => { | ||
const item = { | ||
totalCommentCount: 0, | ||
updates: Array.from({ length: 3 }).fill( | ||
FactoryResearchItemUpdate({ | ||
status: ResearchUpdateStatus.PUBLISHED, | ||
_deleted: false, | ||
}), | ||
), | ||
} as IResearch.ItemDB | ||
expect(getResearchTotalCommentCount(item)).toBe(0) | ||
}) | ||
|
||
it('should return the correct amount of comments', () => { | ||
const item = { | ||
updates: Array.from({ length: 3 }).fill( | ||
FactoryResearchItemUpdate({ | ||
status: ResearchUpdateStatus.PUBLISHED, | ||
_deleted: false, | ||
comments: Array.from({ length: 3 }), | ||
}), | ||
), | ||
} as IResearch.ItemDB | ||
expect(getResearchTotalCommentCount(item)).toBe(9) | ||
}) | ||
|
||
it('should ignore deleted and draft updates', () => { | ||
const item = { | ||
updates: Array.from({ length: 2 }) | ||
.fill( | ||
FactoryResearchItemUpdate({ | ||
status: ResearchUpdateStatus.PUBLISHED, | ||
_deleted: false, | ||
comments: Array.from({ length: 2 }), | ||
}), | ||
) | ||
.concat([ | ||
FactoryResearchItemUpdate({ | ||
status: ResearchUpdateStatus.PUBLISHED, | ||
_deleted: true, | ||
comments: Array.from({ length: 3 }), | ||
}), | ||
FactoryResearchItemUpdate({ | ||
status: ResearchUpdateStatus.DRAFT, | ||
_deleted: false, | ||
comments: Array.from({ length: 6 }), | ||
}), | ||
]), | ||
} as IResearch.ItemDB | ||
expect(getResearchTotalCommentCount(item)).toBe(4) | ||
}) | ||
}) | ||
|
||
describe('Research Update Status Filter', () => { | ||
it('should not show item when deleted', async () => { | ||
// prepare | ||
const authorId = 'author' | ||
const item = { _createdBy: authorId } as IResearch.Item | ||
const update = { _deleted: true } as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update, authorId) | ||
|
||
// assert | ||
expect(show).toEqual(false) | ||
}) | ||
|
||
it('should not show item when deleted and draft', async () => { | ||
// prepare | ||
const authorId = 'author' | ||
const item = { _createdBy: authorId } as IResearch.Item | ||
const update = { | ||
_deleted: true, | ||
status: ResearchUpdateStatus.DRAFT, | ||
} as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update, authorId) | ||
|
||
// assert | ||
expect(show).toEqual(false) | ||
}) | ||
|
||
it('should not show when draft and not author', async () => { | ||
// prepare | ||
const authorId = 'author' | ||
const item = { _createdBy: authorId } as IResearch.Item | ||
const update = { status: ResearchUpdateStatus.DRAFT } as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update, 'not-author') | ||
|
||
// assert | ||
expect(show).toEqual(false) | ||
}) | ||
|
||
it('should not show when draft and not authenticated', async () => { | ||
// prepare | ||
const authorId = 'author' | ||
const item = { _createdBy: authorId } as IResearch.Item | ||
const update = { status: ResearchUpdateStatus.DRAFT } as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update) | ||
|
||
// assert | ||
expect(show).toEqual(false) | ||
}) | ||
|
||
it('should show when not draft and not deleted', async () => { | ||
// prepare | ||
const authorId = 'author' | ||
const item = { _createdBy: authorId } as IResearch.Item | ||
const update = { | ||
status: ResearchUpdateStatus.PUBLISHED, | ||
} as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update) | ||
|
||
// assert | ||
expect(show).toEqual(true) | ||
}) | ||
|
||
it('should show when draft and current user is the author', async () => { | ||
// prepare | ||
const authorId = 'author' | ||
const item = { _createdBy: authorId } as IResearch.Item | ||
const update = { status: ResearchUpdateStatus.DRAFT } as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update, authorId) | ||
|
||
// assert | ||
expect(show).toEqual(true) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { ResearchStatus, ResearchUpdateStatus } from 'oa-shared' | ||
|
||
import type { IResearch } from 'src/models' | ||
|
||
export const getResearchTotalCommentCount = ( | ||
item: IResearch.ItemDB, | ||
): number => { | ||
if (Object.hasOwnProperty.call(item, 'totalCommentCount')) { | ||
return item.totalCommentCount || 0 | ||
} | ||
|
||
if (item.updates) { | ||
const commentOnUpdates = item.updates.reduce((totalComments, update) => { | ||
const updateCommentsLength = | ||
!update._deleted && | ||
update.status !== ResearchUpdateStatus.DRAFT && | ||
update.comments | ||
? update.comments.length | ||
: 0 | ||
return totalComments + updateCommentsLength | ||
}, 0) | ||
|
||
return commentOnUpdates ? commentOnUpdates : 0 | ||
} else { | ||
return 0 | ||
} | ||
} | ||
|
||
export const researchUpdateStatusFilter = ( | ||
item: IResearch.Item, | ||
update: IResearch.Update, | ||
currentUserId?: string, | ||
) => { | ||
return ( | ||
(item._createdBy === currentUserId || | ||
update.status !== ResearchUpdateStatus.DRAFT) && | ||
!update._deleted | ||
) | ||
} | ||
|
||
export const getPublicUpdates = ( | ||
item: IResearch.Item, | ||
currentUserId?: string, | ||
) => { | ||
if (item.updates) { | ||
return item.updates.filter((update) => | ||
researchUpdateStatusFilter(item, update, currentUserId), | ||
) | ||
} else { | ||
return [] | ||
} | ||
} | ||
|
||
export const researchStatusColour = ( | ||
researchStatus?: ResearchStatus, | ||
): string => { | ||
return researchStatus === ResearchStatus.ARCHIVED | ||
? 'lightgrey' | ||
: researchStatus === ResearchStatus.COMPLETED | ||
? 'betaGreen' | ||
: 'accent.base' | ||
} |
Oops, something went wrong.