diff --git a/counter-hw/.gitignore b/counter-hw/.gitignore new file mode 100644 index 000000000..aa13fcfac --- /dev/null +++ b/counter-hw/.gitignore @@ -0,0 +1,25 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# production +/build + +# misc +.DS_Store +.env.local +.env.development.local +.env.test.local +.env.production.local +.idea +.gitignore + +npm-debug.log* +yarn-debug.log* +yarn-error.log* diff --git a/counter-hw/package.json b/counter-hw/package.json new file mode 100644 index 000000000..26646d305 --- /dev/null +++ b/counter-hw/package.json @@ -0,0 +1,34 @@ +{ + "name": "counter-hw", + "version": "0.1.0", + "private": true, + "dependencies": { + "@testing-library/jest-dom": "^4.2.4", + "@testing-library/react": "^9.3.2", + "@testing-library/user-event": "^7.1.2", + "react": "^16.12.0", + "react-dom": "^16.12.0", + "react-scripts": "3.3.0" + }, + "scripts": { + "start": "react-scripts start", + "build": "react-scripts build", + "test": "react-scripts test", + "eject": "react-scripts eject" + }, + "eslintConfig": { + "extends": "react-app" + }, + "browserslist": { + "production": [ + ">0.2%", + "not dead", + "not op_mini all" + ], + "development": [ + "last 1 chrome version", + "last 1 firefox version", + "last 1 safari version" + ] + } +} diff --git a/counter-hw/public/favicon.ico b/counter-hw/public/favicon.ico new file mode 100644 index 000000000..a11777cc4 Binary files /dev/null and b/counter-hw/public/favicon.ico differ diff --git a/counter-hw/public/index.html b/counter-hw/public/index.html new file mode 100644 index 000000000..aa069f27c --- /dev/null +++ b/counter-hw/public/index.html @@ -0,0 +1,43 @@ + + + + + + + + + + + + + React App + + + +
+ + + diff --git a/counter-hw/public/logo192.png b/counter-hw/public/logo192.png new file mode 100644 index 000000000..fc44b0a37 Binary files /dev/null and b/counter-hw/public/logo192.png differ diff --git a/counter-hw/public/logo512.png b/counter-hw/public/logo512.png new file mode 100644 index 000000000..a4e47a654 Binary files /dev/null and b/counter-hw/public/logo512.png differ diff --git a/counter-hw/public/manifest.json b/counter-hw/public/manifest.json new file mode 100644 index 000000000..080d6c77a --- /dev/null +++ b/counter-hw/public/manifest.json @@ -0,0 +1,25 @@ +{ + "short_name": "React App", + "name": "Create React App Sample", + "icons": [ + { + "src": "favicon.ico", + "sizes": "64x64 32x32 24x24 16x16", + "type": "image/x-icon" + }, + { + "src": "logo192.png", + "type": "image/png", + "sizes": "192x192" + }, + { + "src": "logo512.png", + "type": "image/png", + "sizes": "512x512" + } + ], + "start_url": ".", + "display": "standalone", + "theme_color": "#000000", + "background_color": "#ffffff" +} diff --git a/counter-hw/public/robots.txt b/counter-hw/public/robots.txt new file mode 100644 index 000000000..01b0f9a10 --- /dev/null +++ b/counter-hw/public/robots.txt @@ -0,0 +1,2 @@ +# https://www.robotstxt.org/robotstxt.html +User-agent: * diff --git a/counter-hw/src/App.css b/counter-hw/src/App.css new file mode 100644 index 000000000..74b5e0534 --- /dev/null +++ b/counter-hw/src/App.css @@ -0,0 +1,38 @@ +.App { + text-align: center; +} + +.App-logo { + height: 40vmin; + pointer-events: none; +} + +@media (prefers-reduced-motion: no-preference) { + .App-logo { + animation: App-logo-spin infinite 20s linear; + } +} + +.App-header { + background-color: #282c34; + min-height: 100vh; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + font-size: calc(10px + 2vmin); + color: white; +} + +.App-link { + color: #61dafb; +} + +@keyframes App-logo-spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} diff --git a/counter-hw/src/App.js b/counter-hw/src/App.js new file mode 100644 index 000000000..70f579640 --- /dev/null +++ b/counter-hw/src/App.js @@ -0,0 +1,24 @@ +import React, {Component} from 'react'; +import logo from './logo.svg'; +import './App.css'; +import Counter from "./Counter"; +import Clock from "./Clock"; + +class App extends Component { + + render() { + return ( +
+
+ logo + + +

----------------------------------------------

+ +
+
+ ); + } +} + +export default App; \ No newline at end of file diff --git a/counter-hw/src/App.test.js b/counter-hw/src/App.test.js new file mode 100644 index 000000000..4db7ebc25 --- /dev/null +++ b/counter-hw/src/App.test.js @@ -0,0 +1,9 @@ +import React from 'react'; +import { render } from '@testing-library/react'; +import App from './App'; + +test('renders learn react link', () => { + const { getByText } = render(); + const linkElement = getByText(/learn react/i); + expect(linkElement).toBeInTheDocument(); +}); diff --git a/counter-hw/src/Clock.js b/counter-hw/src/Clock.js new file mode 100644 index 000000000..06c2204c9 --- /dev/null +++ b/counter-hw/src/Clock.js @@ -0,0 +1,48 @@ +import React, {Component} from "react"; + +class Clock extends Component { + state = { + date: new Date(), + intervalId: undefined + }; + + /** + * start this clock + */ + start = () => { + this.state.intervalId = setInterval( + () => { + this.setState( + { + date: new Date() + }); + }, 1000); + }; + + /** + * stop this clock + */ + stop = () => { + clearInterval(this.state.intervalId); + }; + + componentDidMount() { + this.start() + } + + componentWillUnmount() { + this.stop() + } + + render() { + return ( +
+

{this.state.date.toLocaleTimeString()}

+

+

+
+ ); + } +} + +export default Clock \ No newline at end of file diff --git a/counter-hw/src/Counter.js b/counter-hw/src/Counter.js new file mode 100644 index 000000000..432db42be --- /dev/null +++ b/counter-hw/src/Counter.js @@ -0,0 +1,50 @@ +import React, {Component} from "react"; + +class Counter extends Component { + state = { + num: 0 + }; + + /** + * increase by props.step + */ + handleInc = () => { + this.setState({ + num: this.state.num + 1 * this.props.step + }); + }; + + /** + * decrease by props.step + */ + handleDec = () => { + if(this.state.num > 0) { + this.setState({ + num: this.state.num - 1 * this.props.step + }); + } + }; + + /** + * reset to zero + */ + handleRst = () => { + this.setState({ + num: 0 + }); + }; + + render() { + return
+

Counter

+

값: {this.state.num}

+

+ + + +

+
; + } +} + +export default Counter; \ No newline at end of file diff --git a/counter-hw/src/index.css b/counter-hw/src/index.css new file mode 100644 index 000000000..ec2585e8c --- /dev/null +++ b/counter-hw/src/index.css @@ -0,0 +1,13 @@ +body { + margin: 0; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', + 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', + sans-serif; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +code { + font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', + monospace; +} diff --git a/counter-hw/src/index.js b/counter-hw/src/index.js new file mode 100644 index 000000000..87d1be551 --- /dev/null +++ b/counter-hw/src/index.js @@ -0,0 +1,12 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import './index.css'; +import App from './App'; +import * as serviceWorker from './serviceWorker'; + +ReactDOM.render(, document.getElementById('root')); + +// If you want your app to work offline and load faster, you can change +// unregister() to register() below. Note this comes with some pitfalls. +// Learn more about service workers: https://bit.ly/CRA-PWA +serviceWorker.unregister(); diff --git a/counter-hw/src/logo.svg b/counter-hw/src/logo.svg new file mode 100644 index 000000000..6b60c1042 --- /dev/null +++ b/counter-hw/src/logo.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/counter-hw/src/serviceWorker.js b/counter-hw/src/serviceWorker.js new file mode 100644 index 000000000..8703ddb7e --- /dev/null +++ b/counter-hw/src/serviceWorker.js @@ -0,0 +1,137 @@ +// This optional code is used to register a service worker. +// register() is not called by default. + +// This lets the app load faster on subsequent visits in production, and gives +// it offline capabilities. However, it also means that developers (and users) +// will only see deployed updates on subsequent visits to a page, after all the +// existing tabs open on the page have been closed, since previously cached +// resources are updated in the background. + +// To learn more about the benefits of this model and instructions on how to +// opt-in, read https://bit.ly/CRA-PWA + +const isLocalhost = Boolean( + window.location.hostname === 'localhost' || + // [::1] is the IPv6 localhost address. + window.location.hostname === '[::1]' || + // 127.0.0.0/8 are considered localhost for IPv4. + window.location.hostname.match( + /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ + ) +); + +export function register(config) { + if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { + // The URL constructor is available in all browsers that support SW. + const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); + if (publicUrl.origin !== window.location.origin) { + // Our service worker won't work if PUBLIC_URL is on a different origin + // from what our page is served on. This might happen if a CDN is used to + // serve assets; see https://github.com/facebook/create-react-app/issues/2374 + return; + } + + window.addEventListener('load', () => { + const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; + + if (isLocalhost) { + // This is running on localhost. Let's check if a service worker still exists or not. + checkValidServiceWorker(swUrl, config); + + // Add some additional logging to localhost, pointing developers to the + // service worker/PWA documentation. + navigator.serviceWorker.ready.then(() => { + console.log( + 'This web app is being served cache-first by a service ' + + 'worker. To learn more, visit https://bit.ly/CRA-PWA' + ); + }); + } else { + // Is not localhost. Just register service worker + registerValidSW(swUrl, config); + } + }); + } +} + +function registerValidSW(swUrl, config) { + navigator.serviceWorker + .register(swUrl) + .then(registration => { + registration.onupdatefound = () => { + const installingWorker = registration.installing; + if (installingWorker == null) { + return; + } + installingWorker.onstatechange = () => { + if (installingWorker.state === 'installed') { + if (navigator.serviceWorker.controller) { + // At this point, the updated precached content has been fetched, + // but the previous service worker will still serve the older + // content until all client tabs are closed. + console.log( + 'New content is available and will be used when all ' + + 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' + ); + + // Execute callback + if (config && config.onUpdate) { + config.onUpdate(registration); + } + } else { + // At this point, everything has been precached. + // It's the perfect time to display a + // "Content is cached for offline use." message. + console.log('Content is cached for offline use.'); + + // Execute callback + if (config && config.onSuccess) { + config.onSuccess(registration); + } + } + } + }; + }; + }) + .catch(error => { + console.error('Error during service worker registration:', error); + }); +} + +function checkValidServiceWorker(swUrl, config) { + // Check if the service worker can be found. If it can't reload the page. + fetch(swUrl, { + headers: { 'Service-Worker': 'script' } + }) + .then(response => { + // Ensure service worker exists, and that we really are getting a JS file. + const contentType = response.headers.get('content-type'); + if ( + response.status === 404 || + (contentType != null && contentType.indexOf('javascript') === -1) + ) { + // No service worker found. Probably a different app. Reload the page. + navigator.serviceWorker.ready.then(registration => { + registration.unregister().then(() => { + window.location.reload(); + }); + }); + } else { + // Service worker found. Proceed as normal. + registerValidSW(swUrl, config); + } + }) + .catch(() => { + console.log( + 'No internet connection found. App is running in offline mode.' + ); + }); +} + +export function unregister() { + if ('serviceWorker' in navigator) { + navigator.serviceWorker.ready.then(registration => { + registration.unregister(); + }); + } +} diff --git a/counter-hw/src/setupTests.js b/counter-hw/src/setupTests.js new file mode 100644 index 000000000..74b1a275a --- /dev/null +++ b/counter-hw/src/setupTests.js @@ -0,0 +1,5 @@ +// jest-dom adds custom jest matchers for asserting on DOM nodes. +// allows you to do things like: +// expect(element).toHaveTextContent(/react/i) +// learn more: https://github.com/testing-library/jest-dom +import '@testing-library/jest-dom/extend-expect'; diff --git a/counter/.gitignore b/counter/.gitignore new file mode 100644 index 000000000..4d29575de --- /dev/null +++ b/counter/.gitignore @@ -0,0 +1,23 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# production +/build + +# misc +.DS_Store +.env.local +.env.development.local +.env.test.local +.env.production.local + +npm-debug.log* +yarn-debug.log* +yarn-error.log* diff --git a/counter/package.json b/counter/package.json new file mode 100644 index 000000000..169f1e82c --- /dev/null +++ b/counter/package.json @@ -0,0 +1,34 @@ +{ + "name": "counter", + "version": "0.1.0", + "private": true, + "dependencies": { + "@testing-library/jest-dom": "^4.2.4", + "@testing-library/react": "^9.4.0", + "@testing-library/user-event": "^7.1.2", + "react": "^16.12.0", + "react-dom": "^16.12.0", + "react-scripts": "3.3.0" + }, + "scripts": { + "start": "react-scripts start", + "build": "react-scripts build", + "test": "react-scripts test", + "eject": "react-scripts eject" + }, + "eslintConfig": { + "extends": "react-app" + }, + "browserslist": { + "production": [ + ">0.2%", + "not dead", + "not op_mini all" + ], + "development": [ + "last 1 chrome version", + "last 1 firefox version", + "last 1 safari version" + ] + } +} diff --git a/counter/public/favicon.ico b/counter/public/favicon.ico new file mode 100644 index 000000000..a11777cc4 Binary files /dev/null and b/counter/public/favicon.ico differ diff --git a/counter/public/index.html b/counter/public/index.html new file mode 100644 index 000000000..aa069f27c --- /dev/null +++ b/counter/public/index.html @@ -0,0 +1,43 @@ + + + + + + + + + + + + + React App + + + +
+ + + diff --git a/counter/public/logo192.png b/counter/public/logo192.png new file mode 100644 index 000000000..fc44b0a37 Binary files /dev/null and b/counter/public/logo192.png differ diff --git a/counter/public/logo512.png b/counter/public/logo512.png new file mode 100644 index 000000000..a4e47a654 Binary files /dev/null and b/counter/public/logo512.png differ diff --git a/counter/public/manifest.json b/counter/public/manifest.json new file mode 100644 index 000000000..080d6c77a --- /dev/null +++ b/counter/public/manifest.json @@ -0,0 +1,25 @@ +{ + "short_name": "React App", + "name": "Create React App Sample", + "icons": [ + { + "src": "favicon.ico", + "sizes": "64x64 32x32 24x24 16x16", + "type": "image/x-icon" + }, + { + "src": "logo192.png", + "type": "image/png", + "sizes": "192x192" + }, + { + "src": "logo512.png", + "type": "image/png", + "sizes": "512x512" + } + ], + "start_url": ".", + "display": "standalone", + "theme_color": "#000000", + "background_color": "#ffffff" +} diff --git a/counter/public/robots.txt b/counter/public/robots.txt new file mode 100644 index 000000000..01b0f9a10 --- /dev/null +++ b/counter/public/robots.txt @@ -0,0 +1,2 @@ +# https://www.robotstxt.org/robotstxt.html +User-agent: * diff --git a/counter/src/App.css b/counter/src/App.css new file mode 100644 index 000000000..74b5e0534 --- /dev/null +++ b/counter/src/App.css @@ -0,0 +1,38 @@ +.App { + text-align: center; +} + +.App-logo { + height: 40vmin; + pointer-events: none; +} + +@media (prefers-reduced-motion: no-preference) { + .App-logo { + animation: App-logo-spin infinite 20s linear; + } +} + +.App-header { + background-color: #282c34; + min-height: 100vh; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + font-size: calc(10px + 2vmin); + color: white; +} + +.App-link { + color: #61dafb; +} + +@keyframes App-logo-spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} diff --git a/counter/src/App.js b/counter/src/App.js new file mode 100644 index 000000000..9ce573974 --- /dev/null +++ b/counter/src/App.js @@ -0,0 +1,42 @@ +//import는 외부의 파일을 불러 오는 명령어 +import React, { Component } from 'react'; +//react를 사용하기 위해서 react 라이브러리에서 React와 Component를 호출함 +import logo from './logo.svg'; +import './App.css'; +//css를 불러옴 파일 이름은 달라도 된다. +import Counter from "./Counter"; +import Clock from "./Clock"; + +//Component라는 react의 class를 상속 받음 +class App extends Component { + state = { + pnum: 0 + }; + handlePInc = () => { + this.setState({ + pnum: this.state.pnum + 1 + }); + }; + //jsx를 화면에 보여줄때 render함수를 호출해서 return을 해야 한다. + //jsx는 js를 html처럼 사용할수 있게 해주는 문법이다. 아래와 같다 + + //counter에서 부모가 props를 통해서 데이터를 전달한다. 안받을 수 없다. + + render() { + return ( +
+
+ logo +

Welcome to React

+ + +
+
+ ); + } +} +//외부에서 import로 불러오기 위해서는 export를 해줘야 한다. +//default는 하나의 모듈 혹은 class만 export할때 써준다. +//https://enyobook.wordpress.com/2016/08/17/export-default%EC%97%90-%EB%8C%80%ED%95%B4/ +export default App; + diff --git a/counter/src/App.test.js b/counter/src/App.test.js new file mode 100644 index 000000000..4db7ebc25 --- /dev/null +++ b/counter/src/App.test.js @@ -0,0 +1,9 @@ +import React from 'react'; +import { render } from '@testing-library/react'; +import App from './App'; + +test('renders learn react link', () => { + const { getByText } = render(); + const linkElement = getByText(/learn react/i); + expect(linkElement).toBeInTheDocument(); +}); diff --git a/counter/src/Clock.js b/counter/src/Clock.js new file mode 100644 index 000000000..e35714706 --- /dev/null +++ b/counter/src/Clock.js @@ -0,0 +1,29 @@ +import React, {Component} from "react"; + +class Clock extends Component { + state = { + date: new Date() + }; + + //CB 호출 순서를 파악하자. + componentDidMount() { + this.intervalId = setInterval( + () => { + this.setState({date: new Date()}); + }, 1000); + } + + componentDidUpdate() { + console.log("update"); + } + + render() { + console.log("render"); + return ( +
+ Clock {this.state.date.toLocaleTimeString()} +
); + } +} + +export default Clock; \ No newline at end of file diff --git a/counter/src/Counter.js b/counter/src/Counter.js new file mode 100644 index 000000000..5d2b0fb5e --- /dev/null +++ b/counter/src/Counter.js @@ -0,0 +1,31 @@ +import React, {Component} from "react"; + +class Counter extends Component { + state = { + num: this.props.number + }; + + //setState를 해야만 화면이 갱신된다. + //this, binding에 대하여 javascript스터디들 해보자. 이 이유 때문에 () => 를 사용한다. + handleInc = () => { + this.setState({ + num: this.state.num + 1 + }); + }; + + render() { + //this.props.number = 10; //props는 변경이 불가능하다. + //this.handeInc()를 하면 첫 랜더링에서 함수가 자동으로 호출된다. 이후에 setState하면서 계속 화면 갱신.. 무한루프로 계속갱신 --> 에러 + return
+ counter +

{this.props.number}

+

{this.state.num}

+

+ + +

+
; + } +} + +export default Counter; \ No newline at end of file diff --git a/counter/src/index.css b/counter/src/index.css new file mode 100644 index 000000000..ec2585e8c --- /dev/null +++ b/counter/src/index.css @@ -0,0 +1,13 @@ +body { + margin: 0; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', + 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', + sans-serif; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +code { + font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', + monospace; +} diff --git a/counter/src/index.js b/counter/src/index.js new file mode 100644 index 000000000..87d1be551 --- /dev/null +++ b/counter/src/index.js @@ -0,0 +1,12 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import './index.css'; +import App from './App'; +import * as serviceWorker from './serviceWorker'; + +ReactDOM.render(, document.getElementById('root')); + +// If you want your app to work offline and load faster, you can change +// unregister() to register() below. Note this comes with some pitfalls. +// Learn more about service workers: https://bit.ly/CRA-PWA +serviceWorker.unregister(); diff --git a/counter/src/logo.svg b/counter/src/logo.svg new file mode 100644 index 000000000..6b60c1042 --- /dev/null +++ b/counter/src/logo.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/counter/src/serviceWorker.js b/counter/src/serviceWorker.js new file mode 100644 index 000000000..8703ddb7e --- /dev/null +++ b/counter/src/serviceWorker.js @@ -0,0 +1,137 @@ +// This optional code is used to register a service worker. +// register() is not called by default. + +// This lets the app load faster on subsequent visits in production, and gives +// it offline capabilities. However, it also means that developers (and users) +// will only see deployed updates on subsequent visits to a page, after all the +// existing tabs open on the page have been closed, since previously cached +// resources are updated in the background. + +// To learn more about the benefits of this model and instructions on how to +// opt-in, read https://bit.ly/CRA-PWA + +const isLocalhost = Boolean( + window.location.hostname === 'localhost' || + // [::1] is the IPv6 localhost address. + window.location.hostname === '[::1]' || + // 127.0.0.0/8 are considered localhost for IPv4. + window.location.hostname.match( + /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ + ) +); + +export function register(config) { + if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { + // The URL constructor is available in all browsers that support SW. + const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); + if (publicUrl.origin !== window.location.origin) { + // Our service worker won't work if PUBLIC_URL is on a different origin + // from what our page is served on. This might happen if a CDN is used to + // serve assets; see https://github.com/facebook/create-react-app/issues/2374 + return; + } + + window.addEventListener('load', () => { + const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; + + if (isLocalhost) { + // This is running on localhost. Let's check if a service worker still exists or not. + checkValidServiceWorker(swUrl, config); + + // Add some additional logging to localhost, pointing developers to the + // service worker/PWA documentation. + navigator.serviceWorker.ready.then(() => { + console.log( + 'This web app is being served cache-first by a service ' + + 'worker. To learn more, visit https://bit.ly/CRA-PWA' + ); + }); + } else { + // Is not localhost. Just register service worker + registerValidSW(swUrl, config); + } + }); + } +} + +function registerValidSW(swUrl, config) { + navigator.serviceWorker + .register(swUrl) + .then(registration => { + registration.onupdatefound = () => { + const installingWorker = registration.installing; + if (installingWorker == null) { + return; + } + installingWorker.onstatechange = () => { + if (installingWorker.state === 'installed') { + if (navigator.serviceWorker.controller) { + // At this point, the updated precached content has been fetched, + // but the previous service worker will still serve the older + // content until all client tabs are closed. + console.log( + 'New content is available and will be used when all ' + + 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' + ); + + // Execute callback + if (config && config.onUpdate) { + config.onUpdate(registration); + } + } else { + // At this point, everything has been precached. + // It's the perfect time to display a + // "Content is cached for offline use." message. + console.log('Content is cached for offline use.'); + + // Execute callback + if (config && config.onSuccess) { + config.onSuccess(registration); + } + } + } + }; + }; + }) + .catch(error => { + console.error('Error during service worker registration:', error); + }); +} + +function checkValidServiceWorker(swUrl, config) { + // Check if the service worker can be found. If it can't reload the page. + fetch(swUrl, { + headers: { 'Service-Worker': 'script' } + }) + .then(response => { + // Ensure service worker exists, and that we really are getting a JS file. + const contentType = response.headers.get('content-type'); + if ( + response.status === 404 || + (contentType != null && contentType.indexOf('javascript') === -1) + ) { + // No service worker found. Probably a different app. Reload the page. + navigator.serviceWorker.ready.then(registration => { + registration.unregister().then(() => { + window.location.reload(); + }); + }); + } else { + // Service worker found. Proceed as normal. + registerValidSW(swUrl, config); + } + }) + .catch(() => { + console.log( + 'No internet connection found. App is running in offline mode.' + ); + }); +} + +export function unregister() { + if ('serviceWorker' in navigator) { + navigator.serviceWorker.ready.then(registration => { + registration.unregister(); + }); + } +} diff --git a/counter/src/setupTests.js b/counter/src/setupTests.js new file mode 100644 index 000000000..74b1a275a --- /dev/null +++ b/counter/src/setupTests.js @@ -0,0 +1,5 @@ +// jest-dom adds custom jest matchers for asserting on DOM nodes. +// allows you to do things like: +// expect(element).toHaveTextContent(/react/i) +// learn more: https://github.com/testing-library/jest-dom +import '@testing-library/jest-dom/extend-expect';