Skip to content
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

매치 상세내역 firebase 연동 #39

Merged
merged 5 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
31 changes: 30 additions & 1 deletion src/components/MatchDetail.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
import { render, screen, waitFor } from "@testing-library/react";
import MatchDetail from "./MatchDetail";
import firebase from "firebase";

describe("Test", () => {
beforeEach(() => {
((firebase.firestore as unknown) as jest.Mock).mockReturnValue({
collection: jest.fn().mockReturnValue({
doc: jest.fn().mockReturnValue({
get: jest.fn().mockResolvedValue({
exists: {},
data: jest.fn().mockReturnValue({
dateTime: { toDate: jest.fn().mockReturnValue(new Date()) },
place: "용산 더베이스",
memberCount: 15,
teamCount: 3,
gender: "여성",
level: "초급",
link: "www.naver.com",
gameType: "match",
fee: 20000,
canPark: true,
canRentShoes: true,
manager: "배성진",
}),
id: "test12345",
}),
}),
}),
});
});

test("renders MatchDetail", async () => {
render(<MatchDetail />);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

가짜 useParameter 값을 줘야해. 그리고 await db.collection("matches").doc(matchId).get();의 matchId 확인 해줘.

expect(useParameter의  id).toBe(.doc이 쓴matchId)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그런데 그 테스트 만들기 너무 어려우면 나중에 #43 처리 할때 해도 돼.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#43 처리할때 같이 처리할게!


await waitFor(async () => {
//더미데이터 제대로 나오는지 확인
expect(screen.getByText(/초급레벨/i)).toBeInTheDocument();
expect(screen.getByText(/풋살화 대여 불가능/i)).toBeInTheDocument();
expect(screen.getByText(/풋살화 대여 가능/i)).toBeInTheDocument();
expect(screen.getByText(/주차 가능/i)).toBeInTheDocument();
expect(screen.getByText(/20,000원/i)).toBeInTheDocument();
});
Expand Down
47 changes: 30 additions & 17 deletions src/components/MatchDetail.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
import React from "react";
import React, { useState, useEffect } from "react";
import { Divider, Button, Tag } from "antd";
import "antd/dist/antd.css";
import "./MatchDetail.css";
import { Match } from "../types";
import Label from "./Label";

const match: Match = {
dateTime: new Date(),
place: "용산",
memberCount: 15,
teamCount: 3,
gender: "여성",
level: "초급",
link: "www.naver.com",
gameType: "gx+match",
fee: 20000,
canPark: true,
canRentShoes: false,
manager: "배성진",
};
import { useHistory, useParams } from "react-router-dom";
import firebase from "firebase";

const MatchDetail = (): JSX.Element => {
return (
const history = useHistory();
const [match, setMatch] = useState<Match | null>(null);
const { id } = useParams<{ id: string }>();

useEffect(() => {
getMatch();
}, [id]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

응. getMatch을 useEffect안에 옮겨줘. 문서 다시 한번 읽어봐

usually you’ll want to declare functions needed by an effect inside of it.

image

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useEffect 안에 옮겨서 해결했는데 나중에 한번더 설명해줘! ㅎㅎ


const getMatch = async () => {
const db = firebase.firestore();
const doc = await db.collection("matches").doc(id).get();
if (!doc.exists) {
window.alert("잘못된 접근입니다. 목록페이지로 돌아갑니다.");
history.push("/");
return;
}
const match = doc.data();
if (match) {
match.dateTime = match.dateTime.toDate();
match.id = doc.id;
setMatch(match as Match);
}
};

return !match ? (
<div></div>
) : (
<div className="matchDetail">
<a href="/">목록으로 돌아가기</a>
<Divider className="divider" />
Expand Down
1 change: 1 addition & 0 deletions src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const mockHistory = {

jest.mock("react-router-dom", () => ({
useHistory: () => mockHistory,
useParams: () => jest.fn(),
}));

export const fireAntEvent = {
Expand Down