This is a modification of theApp.tsx
andApp.test.tsx
CRAs. It usesuseEffect
to get the text from the Golang API.
- Source code:https://github.com/ynwd/mnrp
- Live demo:https://fastro-319406.firebaseapp.com/
.├── cloudbuild.yaml├── cmd│ ├── build│ │ ├── index.gohtml│ │ └── main.go│ └── main.go├── firebase.json├── go.mod├── internal│ ├── app.go│ └── app_test.go├── package.json├── serverless.go└── web └── home ├── craco.config.js ├── package.json ├── public ├── src │ ├── App.css │ ├── App.test.tsx │ ├── App.tsx │ ├── index.css │ ├── index.tsx │ ├── logo.svg │ ├── react-app-env.d.ts │ ├── reportWebVitals.ts │ └── setupTests.ts ├── tailwind.config.js └── tsconfig.json
Backend
Golang API
packageinternalimport("context""github.com/fastrodev/fastrex")funcHandler(reqfastrex.Request,resfastrex.Response){res.Send("The best interface is no interface")}funcCreateApp()fastrex.App{ctx:=context.Background()app:=fastrex.New()app.Ctx(ctx)app.Get("/api",Handler)returnapp}
Entry point
packageserverlessimport("net/http""github.com/ynwd/mnrp/internal")funcMain(whttp.ResponseWriter,r*http.Request){internal.CreateApp().Serverless(true).ServeHTTP(w,r)}
Frontend
App.tsx
importReact,{useEffect,useState}from'react';importlogofrom'./logo.svg';import'./App.css';functionApp(){const[value,setValue]=useState("");useEffect(()=>{asyncfunctiongetText(){letresponse=awaitfetch('/api')constd=awaitresponse.text()setValue(d)}getText()},[value]);return(<divclassName="App"><headerclassName="App-header"><imgsrc={logo}className="App-logo"alt="logo"/><h3>{value}</h3></header></div>);}exportdefaultApp;
App.test.tsx
importReactfrom'react';import{rest}from'msw';import{setupServer}from'msw/node';import{render,screen}from'@testing-library/react';importAppfrom'./App';constserver=setupServer(rest.get('/api',async(req,res,ctx)=>{returnres(ctx.text("The best interface is no interface"));}));beforeAll(()=>server.listen());afterEach(()=>server.resetHandlers());afterAll(()=>server.close());test('loads and displays greeting',async()=>{render(<App/>);constlinkElement=awaitscreen.findByText('The best interface is no interface');screen.debug()expect(linkElement).toBeInTheDocument();});
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse