From afe31f45b240815a94f01ad30a116025158242c9 Mon Sep 17 00:00:00 2001 From: Robert Dargavel Smith Date: Sat, 4 Sep 2021 23:37:32 +0100 Subject: [PATCH] added front end unit tests --- .env | 1 + backend/main.py | 15 +- package.json | 7 +- run_tests.sh | 3 +- src/App.js | 5 +- src/App.test.js | 18 + src/__snapshots__/App.test.js.snap | 1745 +++++++++++++++++ src/components/About.js | 4 +- src/components/Banner.js | 13 +- src/components/CreatePlaylist.js | 11 +- src/components/CreatePlaylist.test.js | 24 + src/components/Settings.js | 14 +- src/components/Settings.test.js | 26 + src/components/Spotify.js | 2 +- src/components/Track.js | 15 +- src/components/Track.test.js | 12 + src/components/TrackSelector.js | 7 +- src/components/TrackSelector.test.js | 13 + .../__snapshots__/CreatePlaylist.test.js.snap | 59 + .../__snapshots__/Track.test.js.snap | 164 ++ .../__snapshots__/TrackSelector.test.js.snap | 192 ++ yarn.lock | 69 +- 22 files changed, 2369 insertions(+), 50 deletions(-) create mode 100644 .env create mode 100644 src/App.test.js create mode 100644 src/__snapshots__/App.test.js.snap create mode 100644 src/components/CreatePlaylist.test.js create mode 100644 src/components/Settings.test.js create mode 100644 src/components/Track.test.js create mode 100644 src/components/TrackSelector.test.js create mode 100644 src/components/__snapshots__/CreatePlaylist.test.js.snap create mode 100644 src/components/__snapshots__/Track.test.js.snap create mode 100644 src/components/__snapshots__/TrackSelector.test.js.snap diff --git a/.env b/.env new file mode 100644 index 0000000..d4ffd51 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +REACT_APP_API_URL='http://localhost:8001/api/v1' diff --git a/backend/main.py b/backend/main.py index 342ba55..379caeb 100644 --- a/backend/main.py +++ b/backend/main.py @@ -58,6 +58,7 @@ def receive_before_update(mapper, connection, target): "http://localhost:8000", "http://localhost:3000", "http://127.0.0.1:8080", + "http://localhost", ] app.add_middleware( @@ -262,15 +263,21 @@ def update_playlist_id(playlist: schemas.PlaylistId, @app.get("/api/v1/latest_playlists") def get_latest_playlists(top_n: int, db: Session = Depends(get_db)): - db_items = db.query(models.Playlist).order_by(desc( - models.Playlist.created)).limit(top_n).all() + try: + db_items = db.query(models.Playlist).order_by(desc( + models.Playlist.created)).limit(top_n).all() + except: + return [] return db_items @app.get("/api/v1/top_playlists") def get_top_playlists(top_n: int, db: Session = Depends(get_db)): - db_items = db.query(models.Playlist).order_by( - desc(models.Playlist.av_rating)).limit(top_n).all() + try: + db_items = db.query(models.Playlist).order_by( + desc(models.Playlist.av_rating)).limit(top_n).all() + except: + return [] return db_items diff --git a/package.json b/package.json index df75772..657d20a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "dependencies": { "@testing-library/jest-dom": "^5.11.4", - "@testing-library/react": "^11.1.0", + "@testing-library/react": "^12.0.0", "@testing-library/user-event": "^12.1.10", "bootstrap": "5.0.2", "bootswatch": "^5.1.0", @@ -56,5 +56,8 @@ "last 1 safari version" ] }, - "proxy": "http://localhost:8000" + "proxy": "http://localhost:8000", + "devDependencies": { + "react-test-renderer": "^17.0.2" + } } diff --git a/run_tests.sh b/run_tests.sh index e304d67..45a96c2 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -1,5 +1,6 @@ #!/bin/sh -yarn build export SQLALCHEMY_DATABASE_URL=sqlite:///:memory: export CUDA_VISIBLE_DEVICES="" pipenv run "pytest backend" +export CI=true +yarn test \ No newline at end of file diff --git a/src/App.js b/src/App.js index 34badfb..af40820 100644 --- a/src/App.js +++ b/src/App.js @@ -1,13 +1,10 @@ // TODO // // frontend: -// unit tests // ico file -// fix warnings for unique key -// fix warning about combining h2 and a in Banner +// fix warnings in test // // backend: -// handle exceptions in db (e.g., no playlists) // set seed in noise // bug in join the dots? diff --git a/src/App.test.js b/src/App.test.js new file mode 100644 index 0000000..b3bc47c --- /dev/null +++ b/src/App.test.js @@ -0,0 +1,18 @@ +import { render, screen, fireEvent } from "@testing-library/react"; +import React from "react"; +import { MemoryRouter } from "react-router-dom"; +import App from "./App"; + +test("Test app routing", async () => { + const { asFragment } = render(, { wrapper: MemoryRouter }); + fireEvent.click(screen.getByText("Create playlist")); + expect(asFragment()).toMatchSnapshot(); + fireEvent.click(screen.getByText("Top rated playlists")); + expect(asFragment()).toMatchSnapshot(); + fireEvent.click(screen.getByText("Latest playlists")); + expect(asFragment()).toMatchSnapshot(); + fireEvent.click(screen.getByText("Search playlists")); + expect(asFragment()).toMatchSnapshot(); + fireEvent.click(screen.getByText("About")); + expect(asFragment()).toMatchSnapshot(); +}); diff --git a/src/__snapshots__/App.test.js.snap b/src/__snapshots__/App.test.js.snap new file mode 100644 index 0000000..e4c3da3 --- /dev/null +++ b/src/__snapshots__/App.test.js.snap @@ -0,0 +1,1745 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Test app routing 1`] = ` + +
+
+ +

+ Choose the waypoints for your musical journey +

+
+
+
+
+ +
+ +
+
+ + + +
+
+
+
+
+
+
+ + + + + + +
+
+
+
+
+