-
-
Notifications
You must be signed in to change notification settings - Fork 402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create API for research search #3987
Open
goratt12
wants to merge
7
commits into
ONEARMY:master
Choose a base branch
from
goratt12:3924-research-remix-backend
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
56be416
feat: moved fetching research to remix backend
goratt12 6476873
fix: fixed import lint issues
goratt12 335a5b9
Merge branch 'master' into 3924-research-remix-backend
goratt12 c79155b
fix: fix lint issues
goratt12 671be5f
Merge branch '3924-research-remix-backend' of https://github.com/gora…
goratt12 ea6c691
fix: fix merge with research SSR
goratt12 8760cc6
fix: fix PR reviews
goratt12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,109 @@ | ||
import '@testing-library/jest-dom/vitest' | ||
|
||
import { ResearchStatus } from 'oa-shared' | ||
import { describe, expect, it, vi } from 'vitest' | ||
|
||
import { exportedForTesting } from './research.service' | ||
|
||
const mockWhere = vi.fn() | ||
const mockOrderBy = vi.fn() | ||
const mockLimit = vi.fn() | ||
vi.mock('firebase/firestore', () => ({ | ||
collection: vi.fn(), | ||
query: vi.fn(), | ||
and: vi.fn(), | ||
where: (path, op, value) => mockWhere(path, op, value), | ||
limit: (limit) => mockLimit(limit), | ||
orderBy: (field, direction) => mockOrderBy(field, direction), | ||
})) | ||
|
||
vi.mock('../../stores/databaseV2/endpoints', () => ({ | ||
DB_ENDPOINTS: { | ||
research: 'research', | ||
researchCategories: 'researchCategories', | ||
}, | ||
})) | ||
|
||
vi.mock('../../config/config', () => ({ | ||
getConfigurationOption: vi.fn(), | ||
FIREBASE_CONFIG: { | ||
apiKey: 'AIyChVN', | ||
databaseURL: 'https://test.firebaseio.com', | ||
projectId: 'test', | ||
storageBucket: 'test.appspot.com', | ||
}, | ||
localStorage: vi.fn(), | ||
SITE: 'unit-tests', | ||
})) | ||
|
||
describe('research.search', () => { | ||
it('searches for text', () => { | ||
// prepare | ||
const words = ['test', 'text'] | ||
|
||
// act | ||
exportedForTesting.createSearchQuery(words, '', 'MostRelevant', null) | ||
|
||
// assert | ||
expect(mockWhere).toHaveBeenCalledWith( | ||
'keywords', | ||
'array-contains-any', | ||
words, | ||
) | ||
import { researchService } from './research.service' | ||
|
||
describe('research.service', () => { | ||
describe('search', () => { | ||
it('fetches research articles based on search criteria', async () => { | ||
// Mock successful fetch response | ||
global.fetch = vi.fn().mockResolvedValue({ | ||
json: () => | ||
Promise.resolve({ | ||
items: [{ id: '1', title: 'Sample Research' }], | ||
total: 1, | ||
}), | ||
}) | ||
|
||
// Call search with mock parameters | ||
const result = await researchService.search( | ||
['sample'], | ||
'science', | ||
'Newest', | ||
null, | ||
) | ||
|
||
// Assert results | ||
expect(result).toEqual({ | ||
items: [{ id: '1', title: 'Sample Research' }], | ||
total: 1, | ||
}) | ||
}) | ||
|
||
it('handles errors in search', async () => { | ||
global.fetch = vi.fn().mockRejectedValue('error') | ||
|
||
const result = await researchService.search( | ||
['sample'], | ||
'science', | ||
'Newest', | ||
null, | ||
) | ||
|
||
expect(result).toEqual({ items: [], total: 0 }) | ||
}) | ||
}) | ||
|
||
it('filters by category', () => { | ||
// prepare | ||
const category = 'cat1' | ||
describe('getResearchCategories', () => { | ||
it('fetches research categories', async () => { | ||
global.fetch = vi.fn().mockResolvedValue({ | ||
json: () => | ||
Promise.resolve({ categories: [{ id: 'cat1', name: 'Science' }] }), | ||
}) | ||
|
||
// act | ||
exportedForTesting.createSearchQuery([], category, 'MostRelevant', null) | ||
const result = await researchService.getResearchCategories() | ||
|
||
// assert | ||
expect(mockWhere).toHaveBeenCalledWith( | ||
'researchCategory._id', | ||
'==', | ||
category, | ||
) | ||
}) | ||
expect(result).toEqual([{ id: 'cat1', name: 'Science' }]) | ||
}) | ||
|
||
it('handles errors in fetching research categories', async () => { | ||
global.fetch = vi.fn().mockRejectedValue('error') | ||
|
||
it('should not call orderBy if sorting by most relevant', () => { | ||
// act | ||
exportedForTesting.createSearchQuery(['test'], '', 'MostRelevant', null) | ||
const result = await researchService.getResearchCategories() | ||
|
||
// assert | ||
expect(mockOrderBy).toHaveBeenCalledTimes(0) | ||
expect(result).toEqual([]) | ||
}) | ||
}) | ||
|
||
it('should call orderBy when sorting is not MostRelevant', () => { | ||
// act | ||
exportedForTesting.createSearchQuery(['test'], '', 'Newest', null) | ||
describe('getDraftCount', () => { | ||
it('fetches draft count for a user', async () => { | ||
global.fetch = vi.fn().mockResolvedValue({ | ||
json: () => Promise.resolve({ total: 5 }), | ||
}) | ||
|
||
// assert | ||
expect(mockOrderBy).toHaveBeenLastCalledWith('_created', 'desc') | ||
}) | ||
const result = await researchService.getDraftCount('user123') | ||
|
||
expect(result).toBe(5) | ||
}) | ||
|
||
it('handles errors in fetching draft count', async () => { | ||
global.fetch = vi.fn().mockRejectedValue('error') | ||
|
||
it('should filter by research status', () => { | ||
// act | ||
exportedForTesting.createSearchQuery( | ||
['test'], | ||
'', | ||
'Newest', | ||
ResearchStatus.COMPLETED, | ||
) | ||
|
||
// assert | ||
expect(mockWhere).toHaveBeenCalledWith( | ||
'researchStatus', | ||
'==', | ||
ResearchStatus.COMPLETED, | ||
) | ||
const result = await researchService.getDraftCount('user123') | ||
|
||
expect(result).toBe(0) | ||
}) | ||
}) | ||
|
||
it('should limit results', () => { | ||
// prepare | ||
const take = 12 | ||
|
||
// act | ||
exportedForTesting.createSearchQuery( | ||
['test'], | ||
'', | ||
'Newest', | ||
null, | ||
undefined, | ||
take, | ||
) | ||
|
||
// assert | ||
expect(mockLimit).toHaveBeenLastCalledWith(take) | ||
describe('getDrafts', () => { | ||
it('fetches research drafts for a user', async () => { | ||
global.fetch = vi.fn().mockResolvedValue({ | ||
json: () => | ||
Promise.resolve({ | ||
items: [{ id: 'draft1', title: 'Draft Research' }], | ||
}), | ||
}) | ||
|
||
const result = await researchService.getDrafts('user123') | ||
|
||
expect(result).toEqual([{ id: 'draft1', title: 'Draft Research' }]) | ||
}) | ||
|
||
it('handles errors in fetching drafts', async () => { | ||
global.fetch = vi.fn().mockRejectedValue('error') | ||
|
||
const result = await researchService.getDrafts('user123') | ||
|
||
expect(result).toEqual([]) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these tests aren't very useful now, because it's just mocking and checking the mocked value, so not testing anything real.
we should instead add cypress and/or UI tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im on it :)