Skip to content

Commit

Permalink
frontend WalkingSkeleton (#88)
Browse files Browse the repository at this point in the history
Co-authored-by: Enya <[email protected]>
  • Loading branch information
Irisloverain and Enya authored Jul 26, 2023
1 parent 77e75d3 commit f193fad
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,9 @@ sketch
.history
.ionide

#cypress
/packages/frontend/cypress/videos/
/packages/frontend/cypress/screenshots/


# End of https://www.toptal.com/developers/gitignore/api/intellij,visualstudiocode,git,java,react,maven,eclipse
8 changes: 4 additions & 4 deletions packages/frontend/cypress.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { defineConfig } = require("cypress");
const { defineConfig } = require('cypress')

module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});
}
}
})
13 changes: 13 additions & 0 deletions packages/frontend/cypress/e2e/APITesting.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// eslint-disable-next-line no-undef
describe('HTTPRequest', () => {
// eslint-disable-next-line no-undef
it('GET Call', () => {
// eslint-disable-next-line no-unused-expressions, no-undef
cy.request(
'GET',
'https://001f08b9-acb7-4c3a-a54f-a9254b7e8e55.mock.pstmn.io/get?msg=hello'
)
.its('status')
.should('equal', 200)
})
})
30 changes: 30 additions & 0 deletions packages/frontend/cypress/e2e/useMockAPI.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// eslint-disable-next-line no-undef
describe('E2E Tests to MockAPI', () => {
// eslint-disable-next-line no-undef
beforeEach(() => {
// 跑E2E測試時,intercept時會攔截XHR、fetch、跨域請求,將get方法呼叫的API改為放在fixtures資料夾的hello.json*
// eslint-disable-next-line no-undef
cy.intercept(
'GET',
'https://001f08b9-acb7-4c3a-a54f-a9254b7e8e55.mock.pstmn.io/get?msg=hello',
{
fixture: 'hello.json'
}
).as('getAPI')
})

// eslint-disable-next-line no-undef
it('should load data from Local Mock API', () => {
// eslint-disable-next-line no-undef
cy.visit('http://localhost:3001/')

// eslint-disable-next-line no-undef
cy.wait('@getAPI', { timeout: 10000 })
.its('response.body')
.then((data) => {
// 在這裡設定預期從API獲取到的數據
// eslint-disable-next-line no-undef
expect(data.args.msg).to.equal('hello')
})
})
})
18 changes: 18 additions & 0 deletions packages/frontend/cypress/fixtures/hello.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"args": {
"msg": "hello"
},
"headers": {
"x-forwarded-proto": "https",
"x-forwarded-port": "443",
"host": "postman-echo.com",
"x-amzn-trace-id": "Root=1-64ac06fd-778a564925e76121259dd571",
"x-api-key": "PMAK-64a766dd07cb240031aee0fa-a5ac0a936c61951cec0d9a531788926d41",
"user-agent": "PostmanRuntime/7.32.3",
"accept": "*/*",
"cache-control": "no-cache",
"postman-token": "c462399d-5328-4c40-be44-c9f6ab7fdf4b",
"accept-encoding": "gzip, deflate, br"
},
"url": "https://postman-echo.com/get?msg=hello"
}
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/frontend/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
2 changes: 1 addition & 1 deletion packages/frontend/cypress/support/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
// require('./commands')
Binary file removed packages/frontend/cypress/videos/spec.cy.js.mp4
Binary file not shown.
21 changes: 21 additions & 0 deletions packages/frontend/js/api/getAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useEffect, useState } from 'react'
import axios from 'axios'

function getAPI() {
const [data, setData] = useState({})
useEffect(() => {
axios({
method: 'GET',
url: 'https://001f08b9-acb7-4c3a-a54f-a9254b7e8e55.mock.pstmn.io/get?msg=hello'
})
.then((res) => {
setData(res.data.args)
})
.catch((error) => {
console.log(error)
})
}, [])
return { data }
}

export default getAPI
10 changes: 8 additions & 2 deletions packages/frontend/js/components/App.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import getAPI from '../api/getAPI'

const App = () => {
return <div>Hello World!</div>
const { data } = getAPI()
return (
<>
<div>title:{data.msg}</div>
</>
)
}

export default App

0 comments on commit f193fad

Please sign in to comment.