Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redefine galleryApi endpoint by introducing library for SignalR testing #107

Open
wants to merge 43 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
3973745
Set up unit tests for gallery ui - app
JanSafronov May 17, 2024
ae20628
Merge pull request #1 from Olszewskidev/main
JanSafronov May 17, 2024
d9bf86c
Setup gallery-ui middleware unit tests
JanSafronov May 18, 2024
6357247
Write one LoginPage unit test
JanSafronov May 18, 2024
f12c618
Merge pull request #5 from Olszewskidev/main
JanSafronov May 18, 2024
76a4279
Set form submit handling unit tests
JanSafronov May 18, 2024
8207e01
Start gallery unit tests
JanSafronov May 18, 2024
08b4398
Fix imports in unit tests
JanSafronov May 18, 2024
7c6a133
Commit last Jest usage
JanSafronov May 20, 2024
95e2900
Replace Jest testing with Vitest and setup unit tests for gallery-ui …
JanSafronov May 21, 2024
4f74c36
Configure a few login tests
JanSafronov May 21, 2024
020fc4c
Fix one unit test
JanSafronov May 23, 2024
3389ccb
Commit a few test fixes
JanSafronov May 23, 2024
4651b18
Fix router home testing
JanSafronov May 24, 2024
f6de966
Merge pull request #10 from Olszewskidev/main
JanSafronov May 24, 2024
9e2b39b
Commit few routing test elements
JanSafronov May 24, 2024
7cce06f
Commit routing test
JanSafronov May 25, 2024
04a0562
Fix router tests
JanSafronov May 25, 2024
b1bd1e6
Merge pull request #16 from Olszewskidev/main
JanSafronov May 25, 2024
82ff47a
Merge pull request #17 from Olszewskidev/main
JanSafronov May 25, 2024
b49e51f
Merge pull request #19 from JanSafronov/feature-1.1
JanSafronov May 25, 2024
bde4a0a
Merge pull request #19 from JanSafronov/feature-1.1
JanSafronov May 25, 2024
47daeb7
Merge branch 'feature-1' of https://github.com/JanSafronov/lens-up in…
JanSafronov May 25, 2024
de9e45f
Fix testing and setup a few Home tests
JanSafronov May 26, 2024
8720cbc
Fix pages tests
JanSafronov May 27, 2024
292cbdf
Fix ui gallery tests
JanSafronov May 27, 2024
5e99606
Fix gallery ui tests and start setting photo collector ui
JanSafronov May 30, 2024
36e5344
Merge branch 'main' of https://github.com/Olszewskidev/lens-up into O…
JanSafronov May 30, 2024
fba4e07
Merge branch 'Olszewskidev-main' into feature-1
JanSafronov May 30, 2024
3992308
Merge pull request #24 from Olszewskidev/main
JanSafronov Jun 1, 2024
0ce7d26
fix photo addition form testing
JanSafronov Jun 1, 2024
1ca5002
Fix photo addition form tests
JanSafronov Jun 1, 2024
44771d5
Fix collector api tests
JanSafronov Jun 1, 2024
ef1f6ed
Fix tests and prepare hub integration test for home page
JanSafronov Jun 2, 2024
36f77e0
Start hub mock
JanSafronov Jun 2, 2024
4aedfa3
Merge pull request #28 from Olszewskidev/main
JanSafronov Jun 3, 2024
aa124f1
Set home tests without Cypress hub mocking
JanSafronov Jun 3, 2024
75fe3a5
Redefine getGalleryPhotos endpoint to mock SignalR hub with Cypress d…
JanSafronov Jun 3, 2024
5f28c1c
Merge branch 'main' of https://github.com/Olszewskidev/lens-up into O…
JanSafronov Jun 3, 2024
8c86add
Merge branch 'Olszewskidev-main' into feature-1.1
JanSafronov Jun 3, 2024
cd7fdfc
Redefine getGalleryPhotos endpoint by applying Cypress library for Si…
JanSafronov Jun 4, 2024
506d7dc
Cleanup a bit home tests
JanSafronov Jun 4, 2024
d06df15
Remove unnecessary image
JanSafronov Jun 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests and prepare hub integration test for home page
  • Loading branch information
JanSafronov committed Jun 2, 2024
commit ef1f6edefc338bb1d56db934cb8dbc7539c54297
1,606 changes: 1,604 additions & 2 deletions ui-applications/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ui-applications/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"babel-jest": "^29.7.0",
"babel-plugin-transform-import-meta": "^2.2.1",
"babel-preset-vite": "^1.1.3",
"cypress-signalr-mock": "^1.5.0",
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
Expand Down
3 changes: 2 additions & 1 deletion ui-applications/packages/gallery-ui/.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
VITE_GALLERY_SERVICE_URL=https://localhost:3000
VITE_GALLERY_SERVICE_URL=https://localhost:3000
VITE_PHOTO_COLLECTOR_SERVICE_URL=https://localhost:8081
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export const galleryApi = createApi({
});
});

socket.send("PhotoUploadedToGallery", new File(["happy"], "happy.png", { type: "image/png" }))

await cacheEntryRemoved;

socket.off('connect');
Expand Down
136 changes: 107 additions & 29 deletions ui-applications/packages/gallery-ui/tests/pages/Home/Home.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import { cleanup, fireEvent, getAllByAltText, getByAltText, getByText, render, screen, waitFor } from '@testing-library/react'
import { expect, test, describe, beforeAll, beforeEach ,afterEach, afterAll } from 'vitest';
import { buildQueries, cleanup, fireEvent, getByAltText, getSuggestedQuery, render, screen, waitFor } from '@testing-library/react'
import { expect, test, describe, vi, beforeAll, beforeEach, afterEach, afterAll } from 'vitest';
import { useCypressSignalRMock as MockHubConnectionBuild } from 'cypress-signalr-mock';
import { HubConnectionBuilder } from '@microsoft/signalr';
import { setupServer } from 'msw/node';
import { loginSubmit } from '../../utils/LoginEvents.tsx';
import { getQRCodeUrl } from '../../../src/utils/qRCodeHelper.ts';
import { HttpResponse, http as mswhttp } from 'msw';
import { LoginToGalleryResponse } from '../../../src/types/GalleryApiTypes.ts';
import { Server, Socket } from "socket.io";
import { io as ioc, Socket as ClientSocket } from "socket.io-client";
import { IncomingMessage, ServerResponse, createServer } from "http";
import { io as ioc, Socket as ClientSocket } from "socket.io-client";
import { IncomingMessage, ServerResponse } from "http";
import http from "http";
import { AddressInfo } from 'node:net';
import { startServer } from "../../webSocketTestUtils.tsx";
import { store } from '../../../src/app/store/store.ts';
import { store as photoStore } from '../../../../photo-collector-ui/src/app/store.ts';
import { Provider } from 'react-redux'
import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom';
import AddPhotoToGalleryPage from "../../../../photo-collector-ui/src/pages/AddPhotoToGallery/AddPhotoToGalleryPage.tsx";
import { ErrorCardComponent, SuccessCardComponent } from '@lens-up/shared-components';
import { HomePage, LoginPage } from '../../../src/pages/index.ts';
import { galleryApi, useGetGalleryPhotosQuery } from '../../../src/services/GalleryApi.tsx';

const photo = new File(["happy"], "happy.png", { type: "image/png" });

function waitForSocket(socket: Socket | ClientSocket, event: string) {
return new Promise((resolve) => {
Expand All @@ -21,14 +32,34 @@ function waitForSocket(socket: Socket | ClientSocket, event: string) {

let handlers = [
mswhttp.post(`${import.meta.env.VITE_GALLERY_SERVICE_URL}/login`, async () => {
let response: LoginToGalleryResponse = { enterCode: '0', galleryId: '0', qrCodeUrl: 'QRCodeUrl' };
let res = await HttpResponse.json(response);
return res;
let response: LoginToGalleryResponse = { enterCode: '0', galleryId: '0', qrCodeUrl: 'QRCodeUrl' };
let res = await HttpResponse.json(response);
return res;
}),
mswhttp.post(`${import.meta.env.VITE_PHOTO_COLLECTOR_SERVICE_URL}/upload-photo/0`, async () => {
const socket = new HubConnectionBuilder().withUrl(`${import.meta.env.VITE_GALLERY_SERVICE_URL}/hubs/gallery?galleryId=0`).build();

await socket.start().then(() => {
console.log("Connected to socket.")
}).catch(() => {
console.error("Failed during socket connection.")
})

socket.send("PhotoUploadedToGallery", photo);

return new Response(null, {
status: 200,
headers: {
Allow: 'GET,HEAD,POST',
},
})
})
]

let asFragment: () => DocumentFragment;
let HomeasFragment: () => DocumentFragment;
let baseElement: HTMLElement;
let homeRender: DocumentFragment;

/**
* @vitest-environment jsdom
Expand All @@ -42,10 +73,11 @@ describe("Home page with no photos", async () => {
})
beforeEach(async () => waitFor(() => {
expect(global.window.location.pathname).contains("/gallery/0");
homeRender = asFragment();
}))
afterEach(cleanup);
afterEach(cleanup);
afterAll(() => {
server.close()
server.close()
})

test("No photo hub must show qr code card in home page", async () => {
Expand All @@ -63,47 +95,93 @@ describe("Home page with no photos", async () => {
});
});

const successCardRender = render(
<SuccessCardComponent title="Congratulations!" text="You just joined to the party." />
).asFragment();

const mock = vi.spyOn(galleryApi.endpoints.getGalleryPhotos, 'useQuery');

mock.mockImplementation((stri, skip) => );


describe("Home page with photos", async () => {
let io: Server;

let serverSocket: Socket | undefined;
let clientSocket: ClientSocket;

/*handlers.concat(mswhttp.post(`${import.meta.env.VITE_GALLERY_SERVICE_URL}/hubs/gallery`, async (galleryId) => {
let response: LoginToGalleryResponse = { enterCode: '0', galleryId: '0', qrCodeUrl: 'QRCodeUrl' };
let res = await HttpResponse.json(response);
return res;
}))*/

const server = setupServer(...handlers);
var Sserver: http.Server<typeof IncomingMessage, typeof ServerResponse>;

beforeAll(async (done) => {
Sserver = await startServer(3000);

beforeAll(async () => {
//Sserver = await startServer(3000);

server.listen();
({ asFragment, baseElement } = await loginSubmit());
HomeasFragment = asFragment;
console.log(global.window.location.href);
})
beforeEach(async () => waitFor(() => {
expect(global.window.location.pathname).contains("/gallery/0");
//homeRender = asFragment();
}))
afterEach(cleanup);
afterEach(cleanup);
afterAll(() => {
server.close()
Sserver.close()
server.close()
//Sserver.close()
})

test("Photo gallery must be shown in home", async () => {
URL.createObjectURL = vi.fn();
const { container, baseElement, asFragment } = render(
<Provider store={store}>
<BrowserRouter>
<Routes>
<Route path="/gallery/:enterCode" element={<HomePage />} />
</Routes>
<Navigate to="/gallery/0" replace={true} />
</BrowserRouter>
</Provider>
);

expect(asFragment()).toMatchSnapshot();
});

test("Photo gallery must be shown", async () => {
clientSocket.emit("multiply-by-2", 5);
console.log(global.window.location.href);
URL.createObjectURL = vi.fn();
const { container, baseElement, asFragment } = render(

const promises = [
waitForSocket(clientSocket, "multiply-by-2"),
];
<Provider store={photoStore}>
<BrowserRouter>
<Routes>
<Route path="/upload-photo/:enterCode" element={<AddPhotoToGalleryPage />} />
</Routes>
<Navigate to="/upload-photo/0" replace={true} />
</BrowserRouter>
</Provider>

const [response] = await Promise.all(promises);
const firstRender = asFragment();
);

expect(firstRender).toMatchSnapshot();
});
const element = container.querySelector("#dropzone-file") as HTMLElement;

console.log(global.window.location.href);

fireEvent.change(element, { target: { files: [photo] } });

fireEvent.change(screen.getByPlaceholderText("Your name"), { target: { value: "author" } });

fireEvent.change(screen.getByPlaceholderText("..."), { target: { value: "happy" } });

fireEvent.submit(screen.getByRole('button'));

let successRender = asFragment();

await waitFor(() => {
successRender = asFragment();
expect(successRender).toEqual(successCardRender);
}, { timeout: 50000 }).then(() => expect(asFragment()).toMatchSnapshot());

//const [response] = await Promise.all(promises);
}, { timeout: 50000 });
})

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const baseQuery = fetchBaseQuery({
baseUrl: import.meta.env.VITE_PHOTO_COLLECTOR_SERVICE_URL,
});

const temp = import.meta.env.VITE_PHOTO_COLLECTOR_SERVICE_URL;

export const photoCollectorApi = createApi({
reducerPath: 'photoCollectorApi',
baseQuery: baseQuery,
Expand Down
Loading