Skip to content
Draft
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
25 changes: 25 additions & 0 deletions nuxt-pinia-tailwind/store/repoStore/repoStore.spec.ts
Original file line number Diff line number Diff line change
@@ -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
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineStore } from 'pinia';
import {
IBranch,
IComments,
IIssue,
IPullRequest,
IReadme,
Expand Down Expand Up @@ -51,11 +52,7 @@ export const useRepoStore = defineStore('repositoryStore ', {
// Prepare the promises to be fetched
const getRepoInfo = $axios.get<IRepository>(url);
const getRepoBranches = $axios.get<IBranch[]>(branchesUrl);
const getPullRequests = $axios.get<IPullRequest[]>(pullRequestsUrl, {
params: {
state: 'all',
},
});
const getPullRequests = $axios.get<IPullRequest[]>(pullRequestsUrl);
const getRepoRootContents =
$axios.get<IRepoContents[]>(repoContentsUrl);
const getRepoReadme = $axios.get<IReadme>(repoReadmeUrl);
Expand Down Expand Up @@ -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<IComments>(`${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');
}
},
},
});
26 changes: 26 additions & 0 deletions nuxt-pinia-tailwind/test/__mocks__/constants/repositoriesMocks.ts
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 3 additions & 1 deletion nuxt-pinia-tailwind/test/__mocks__/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const handlers = [];
import { repoMock } from '@/test/__mocks__/handlers/repositories';

export const handlers = [repoMock];
12 changes: 12 additions & 0 deletions nuxt-pinia-tailwind/test/__mocks__/handlers/repositories.ts
Original file line number Diff line number Diff line change
@@ -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<IRepository>(
'https://api.github.com/repos/test/test-repo',
(_, res, ctx) => {
return res(ctx.status(200), ctx.json(repositoryMock));
}
);

export default { repoMock };
19 changes: 19 additions & 0 deletions nuxt-pinia-tailwind/types/repository/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export interface IPullRequest {
merged_at: string | null;
review_comments_url: string;
comments: any;
base: {
repo: IRepository;
};
}

export interface IRepoContents {
Expand Down Expand Up @@ -111,3 +114,19 @@ export interface IIssue {
updated_at: string;
closed_by: Partial<IUser>;
}

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';
}