Skip to content

Commit

Permalink
レビュー指摘箇所の修正
Browse files Browse the repository at this point in the history
  • Loading branch information
Azu0925 committed Apr 1, 2022
1 parent 37fed72 commit 557ef85
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 171 deletions.
90 changes: 48 additions & 42 deletions tests/mock/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,46 +41,52 @@ const mockResponse = (
url: string,
serverResponse: any,
code?: number,
): Response => ({
headers: {} as any,
ok: true,
redirected: false,
status: code ?? 200,
statusText: statusTextByCode[code ?? 200] ?? 'OK',
trailer: {} as any,
type: 'default',
body: null,
url,
bodyUsed: false,
arrayBuffer() {
throw new Error('unimplemented; You are doing something wrong.')
},
blob() {
throw new Error('unimplemented; You are doing something wrong.')
},
formData() {
throw new Error('unimplemented; You are doing something wrong.')
},
json() {
return Promise.resolve(JSON.parse(JSON.stringify(serverResponse)))
},
text() {
return Promise.resolve(JSON.stringify(serverResponse))
},
clone() {
throw new Error('unimplemented; You are doing something wrong.')
},
} as Response)
): Response =>
({
headers: {} as any,
ok: true,
redirected: false,
status: code ?? 200,
statusText: statusTextByCode[code ?? 200] ?? 'OK',
trailer: {} as any,
type: 'default',
body: null,
url,
bodyUsed: false,
arrayBuffer() {
throw new Error('unimplemented; You are doing something wrong.')
},
blob() {
throw new Error('unimplemented; You are doing something wrong.')
},
formData() {
throw new Error('unimplemented; You are doing something wrong.')
},
json() {
return Promise.resolve(JSON.parse(JSON.stringify(serverResponse)))
},
text() {
return Promise.resolve(JSON.stringify(serverResponse))
},
clone() {
throw new Error('unimplemented; You are doing something wrong.')
},
} as Response)

const randomImageTest = /^(https?)?:\/\/dog.ceo\/api\/breeds\/image\/random\/?$/
const randomImageTestByBreed = /^(https?)?:\/\/dog.ceo\/api\/breed\/([A-Za-z]+)(\/([A-Za-z]+))?\/image\/random\/?$/
const randomImageTestByBreed =
/^(https?)?:\/\/dog.ceo\/api\/breed\/([A-Za-z]+)(\/([A-Za-z]+))?\/image\/random\/?$/
const breedsAllTest = /^(https?)?:\/\/dog.ceo\/api\/breeds\/list\/all\/?$/
const randomMultipleImageTestByBreed = /^(https?)?:\/\/dog.ceo\/api\/breed\/([A-Za-z]+)(\/([A-Za-z]+))?\/images\/random\/([1-9]*[0-9]){1,}\/?$/
const randomMultipleImageTestByBreed =
/^(https?)?:\/\/dog.ceo\/api\/breed\/([A-Za-z]+)(\/([A-Za-z]+))?\/images\/random\/(0|[1-9][0-9]*)\/?$/

// not used
const randomMultipleImageTest = /^(https?)?:\/\/dog.ceo\/api\/breeds\/image\/random\/([1-9]*[0-9])\/?$/
const breedImagesTest = /^(https?)?:\/\/dog.ceo\/api\/breed\/([A-Za-z]+)\/images\/?$/
const breedListTest = /^(https?)?:\/\/dog.ceo\/api\/breed\/([A-Za-z]+)\/list\/?$/
const randomMultipleImageTest =
/^(https?)?:\/\/dog.ceo\/api\/breeds\/image\/random\/([1-9]*[0-9])\/?$/
const breedImagesTest =
/^(https?)?:\/\/dog.ceo\/api\/breed\/([A-Za-z]+)\/images\/?$/
const breedListTest =
/^(https?)?:\/\/dog.ceo\/api\/breed\/([A-Za-z]+)\/list\/?$/

const pickOne = <T>(a: T[]): T | undefined => {
if (a.length === 0) {
Expand Down Expand Up @@ -171,25 +177,25 @@ const mockApiRoutes: {
},
{
test: randomMultipleImageTestByBreed,
handle(url: string){
const index = url.match('\d{1,}')
if(index === null){
handle(url: string) {
const matchResult = url.match(randomMultipleImageTestByBreed)
if (matchResult === null) {
return {
status: 'error',
message: 'No number specified',
code: 404
code: 404,
}
}
return {
message: Array.from({length: Number(index)}, () => imageUrl),
status: 'sucess'
message: Array.from({ length: Number(matchResult[5]) }, () => imageUrl),
status: 'sucess',
}
},
},
{
test: breedListTest,
handle: unimplementedMockApiRouteHandler,
}
},
]

export const fetchMock: typeof window.fetch = (resource, ..._) => {
Expand Down
6 changes: 4 additions & 2 deletions tests/station07.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ describe('<App />', () => {
throw new Error('a <header> does not exist')
}

expect(compareColor(window.getComputedStyle(header).backgroundColor, '#f5f5f5')).toBe(true)
expect(
compareColor(window.getComputedStyle(header).backgroundColor, '#f5f5f5'),
).toBe(true)
})
})
})
4 changes: 2 additions & 2 deletions tests/utils/compareColor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Compares two colors. Requires DOM.
*
*
* @param a A color.
* @param b Another color.
* @returns True when the `a` and `b` is identical.
Expand All @@ -19,4 +19,4 @@ export const compareColor = (a: string, b: string): boolean => {
document.body.removeChild(elm)

return res
}
}
Loading

0 comments on commit 557ef85

Please sign in to comment.