diff --git a/nuxt-pinia-tailwind/store/repoStore/repoStore.spec.ts b/nuxt-pinia-tailwind/store/repoStore/repoStore.spec.ts new file mode 100644 index 000000000..d709ccb6d --- /dev/null +++ b/nuxt-pinia-tailwind/store/repoStore/repoStore.spec.ts @@ -0,0 +1,25 @@ +import { setActivePinia, createPinia } from 'pinia'; +import { useRepoStore } from '@/store/repoStore/repoStore'; +import { mswServer } from '@/test/__mocks__/mswServer'; + +beforeAll(() => mswServer.listen()); +afterEach(() => mswServer.resetHandlers()); +afterAll(() => mswServer.close()); + +describe('RepoStore', () => { + beforeEach(() => { + // creates a fresh pinia and make it active so it's automatically picked + // up by any useStore() call without having to pass it to it: + // `useStore(pinia)` + setActivePinia(createPinia()); + }); + + it('should get the repo info', async () => { + const repoStore = useRepoStore(); + + await repoStore.getRepoInfo('test', 'test-repo'); + + expect(repoStore.selectedRepository?.name).toBe('test-repo'); + // TODO: add more assertions + }); +}); diff --git a/nuxt-pinia-tailwind/store/repoStore.ts b/nuxt-pinia-tailwind/store/repoStore/repoStore.ts similarity index 79% rename from nuxt-pinia-tailwind/store/repoStore.ts rename to nuxt-pinia-tailwind/store/repoStore/repoStore.ts index daf9afdf4..fa254623c 100644 --- a/nuxt-pinia-tailwind/store/repoStore.ts +++ b/nuxt-pinia-tailwind/store/repoStore/repoStore.ts @@ -1,6 +1,7 @@ import { defineStore } from 'pinia'; import { IBranch, + IComments, IIssue, IPullRequest, IReadme, @@ -51,11 +52,7 @@ export const useRepoStore = defineStore('repositoryStore ', { // Prepare the promises to be fetched const getRepoInfo = $axios.get(url); const getRepoBranches = $axios.get(branchesUrl); - const getPullRequests = $axios.get(pullRequestsUrl, { - params: { - state: 'all', - }, - }); + const getPullRequests = $axios.get(pullRequestsUrl); const getRepoRootContents = $axios.get(repoContentsUrl); const getRepoReadme = $axios.get(repoReadmeUrl); @@ -129,5 +126,35 @@ export const useRepoStore = defineStore('repositoryStore ', { throw new Error('Error fetching repository issues'); } }, + async getPullRequestsComments(pullRequest: IPullRequest[]) { + try { + const { $axios } = this.$nuxt; + + const commentsPromises = pullRequest.map((p) => { + return $axios.get(`${p.review_comments_url}`); + }); + + const comments = await Promise.all(commentsPromises); + + // Merge the comments with the pull requests + const pullRequestsWithComments = pullRequest.map((p, index) => ({ + ...p, + comments: comments[index].data, + })); + + if (this.selectedRepository) { + this.selectedRepository = { + ...this.selectedRepository, + pullsRequests: pullRequestsWithComments, + }; + } + } catch (error: any) { + if (error && error?.response) { + throw error; + } + + throw new Error('Error fetching pull request comments'); + } + }, }, }); diff --git a/nuxt-pinia-tailwind/test/__mocks__/constants/repositoriesMocks.ts b/nuxt-pinia-tailwind/test/__mocks__/constants/repositoriesMocks.ts new file mode 100644 index 000000000..5d994487d --- /dev/null +++ b/nuxt-pinia-tailwind/test/__mocks__/constants/repositoriesMocks.ts @@ -0,0 +1,26 @@ +import { IRepository } from '@/types/repository/interfaces'; + +export const repositoryMock: IRepository = { + id: 873328, + private: false, + name: 'test-repo', + full_name: 'test/test-repo', + description: 'test description', + owner: { + login: 'test', + }, + html_url: '', + url: '', + updated_at: new Date('2021-05-01T12:00:00Z'), + stargazers_count: 1, + language: 'typescript', + branches_url: '', + visibility: 'public', + subscribers_count: 1, + forks_count: 1, + open_issues_count: 1, + pulls: 1, + default_branch: 'main', + homepage: '', + watchers_count: 1, +}; diff --git a/nuxt-pinia-tailwind/test/__mocks__/handlers/index.ts b/nuxt-pinia-tailwind/test/__mocks__/handlers/index.ts index 0f3710c6e..cab3b4f0a 100644 --- a/nuxt-pinia-tailwind/test/__mocks__/handlers/index.ts +++ b/nuxt-pinia-tailwind/test/__mocks__/handlers/index.ts @@ -1 +1,3 @@ -export const handlers = []; +import { repoMock } from '@/test/__mocks__/handlers/repositories'; + +export const handlers = [repoMock]; diff --git a/nuxt-pinia-tailwind/test/__mocks__/handlers/repositories.ts b/nuxt-pinia-tailwind/test/__mocks__/handlers/repositories.ts new file mode 100644 index 000000000..29d4b72d2 --- /dev/null +++ b/nuxt-pinia-tailwind/test/__mocks__/handlers/repositories.ts @@ -0,0 +1,12 @@ +import { rest } from 'msw'; +import { IRepository } from '@/types/repository/interfaces'; +import { repositoryMock } from '@/test/__mocks__/constants/repositoriesMocks'; + +export const repoMock = rest.get( + 'https://api.github.com/repos/test/test-repo', + (_, res, ctx) => { + return res(ctx.status(200), ctx.json(repositoryMock)); + } +); + +export default { repoMock }; diff --git a/nuxt-pinia-tailwind/types/repository/interfaces.ts b/nuxt-pinia-tailwind/types/repository/interfaces.ts index 6dff9f275..0db1e907b 100644 --- a/nuxt-pinia-tailwind/types/repository/interfaces.ts +++ b/nuxt-pinia-tailwind/types/repository/interfaces.ts @@ -37,6 +37,9 @@ export interface IPullRequest { merged_at: string | null; review_comments_url: string; comments: any; + base: { + repo: IRepository; + }; } export interface IRepoContents { @@ -111,3 +114,19 @@ export interface IIssue { updated_at: string; closed_by: Partial; } + +export interface IComments { + body: string; + user: IUser; + created_at: string; + updated_at: string; + author_association: + | 'COLLABORATOR' + | 'CONTRIBUTOR' + | 'FIRST_TIMER' + | 'FIRST_TIME_CONTRIBUTOR' + | 'MANNEQUIN' + | 'MEMBER' + | 'NONE' + | 'OWNER'; +}