Skip to content

Commit

Permalink
format and fix command
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbjoralt committed Oct 11, 2023
1 parent 5ad3987 commit 5eaa59c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 58 deletions.
18 changes: 10 additions & 8 deletions frontend/mockdata/mockConsultants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Variant } from "@/types";

export const MockConsultants: Variant[] = [{
id: 'id',
name: 'Test Consultant',
email: '[email protected]',
competences: ['Frontend'],
department: 'My Department',
bookings: [{year: 2023, weekNumber: 10, bookedHours: 10}]
}]
export const MockConsultants: Variant[] = [
{
id: "id",
name: "Test Consultant",
email: "[email protected]",
competences: ["Frontend"],
department: "My Department",
bookings: [{ year: 2023, weekNumber: 10, bookedHours: 10 }],
},
];
26 changes: 13 additions & 13 deletions frontend/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig, devices } from '@playwright/test';
import { defineConfig, devices } from "@playwright/test";

/**
* Read environment variables from file.
Expand All @@ -10,7 +10,7 @@ import { defineConfig, devices } from '@playwright/test';
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests',
testDir: "./tests",
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
Expand All @@ -20,31 +20,31 @@ export default defineConfig({
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
reporter: "html",
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: 'http://127.0.0.1:3000',
baseURL: "http://127.0.0.1:3000",

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
trace: "on-first-retry",
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
name: "chromium",
use: { ...devices["Desktop Chrome"] },
},

{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
name: "firefox",
use: { ...devices["Desktop Firefox"] },
},

{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
name: "webkit",
use: { ...devices["Desktop Safari"] },
},

/* Test against mobile viewports. */
Expand All @@ -70,8 +70,8 @@ export default defineConfig({

/* Run your local dev server before starting the tests */
webServer: {
command: 'yarn dev',
url: 'http://127.0.0.1:3000',
command: "yarn start-test",
url: "http://127.0.0.1:3000",
reuseExistingServer: !process.env.CI,
},
});
11 changes: 5 additions & 6 deletions frontend/src/auth/fetchWithToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { loginRequest } from "@/authConfig";
import { MockConsultants } from "../../mockdata/mockConsultants";

export async function fetchWithToken(path: string) {
if(process.env.NEXT_PUBLIC_NO_AUTH){
if (process.env.NEXT_PUBLIC_NO_AUTH) {
return mockedCall(path);
}

Expand Down Expand Up @@ -45,9 +45,8 @@ export async function fetchWithToken(path: string) {
}
}

function mockedCall(path: string){
if(path.includes('/variants')){
return MockConsultants;
}
function mockedCall(path: string) {
if (path.includes("/variants")) {
return MockConsultants;
}
}

15 changes: 11 additions & 4 deletions frontend/src/components/PageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ export default function PageLayout({
}

function Authenticated({ children }: { children: React.ReactNode }) {
return process.env.NEXT_PUBLIC_NO_AUTH ? <>{children}</> : <AuthenticatedTemplate>{children}</AuthenticatedTemplate>;
return process.env.NEXT_PUBLIC_NO_AUTH ? (
<>{children}</>
) : (
<AuthenticatedTemplate>{children}</AuthenticatedTemplate>
);
}

function Unauthenticated({ children }: { children: React.ReactNode }) {
return process.env.NEXT_PUBLIC_NO_AUTH ?
<></> : <UnauthenticatedTemplate>{children}</UnauthenticatedTemplate>;
}
return process.env.NEXT_PUBLIC_NO_AUTH ? (
<></>
) : (
<UnauthenticatedTemplate>{children}</UnauthenticatedTemplate>
);
}
46 changes: 26 additions & 20 deletions frontend/src/hooks/useVibesApi.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
"use client"
import { Variant } from '@/types';
import { fetchWithToken } from '@/auth/fetchWithToken';
import { useIsAuthenticated } from '@azure/msal-react';
"use client";
import { Variant } from "@/types";
import { fetchWithToken } from "@/auth/fetchWithToken";
import { useIsAuthenticated } from "@azure/msal-react";
import { useQuery, useQueryClient } from "react-query";
import { useEffect } from "react";

function useVibesApi(includeOccupied: boolean) {
const isAuthenticated = useIsAuthenticated() || process.env.NEXT_PUBLIC_NO_AUTH;
const isAuthenticated =
useIsAuthenticated() || process.env.NEXT_PUBLIC_NO_AUTH;
const client = useQueryClient();

//TODO: We need a better way of handling state/cache. This works for now though, but it's a bit hacky ngl
useEffect(()=> client.clear(), [includeOccupied, client])
useEffect(() => client.clear(), [includeOccupied, client]);

return useQuery({queryKey: 'vibes', queryFn: async () => {
if (isAuthenticated) {
try {
const response: Variant[] = await fetchWithToken(`/api/v0/variants?weeks=8&includeOccupied=${includeOccupied}`);
return response;

} catch (err) {
console.error(err)
return []
return useQuery({
queryKey: "vibes",
queryFn: async () => {
if (isAuthenticated) {
try {
const response: Variant[] = await fetchWithToken(
`/api/v0/variants?weeks=8&includeOccupied=${includeOccupied}`,
);
return response;
} catch (err) {
console.error(err);
return [];
}
}
}
// If not authenticated, return an empty array
return [];
}, refetchOnWindowFocus: false
})}
// If not authenticated, return an empty array
return [];
},
refetchOnWindowFocus: false,
});
}

export default useVibesApi;
12 changes: 5 additions & 7 deletions frontend/tests/consultant-list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { test, expect } from '@playwright/test';
import { test, expect } from "@playwright/test";

test('has title', async ({ page }) => {
await page.goto('/');
const consultantName = await page.getByText('Test Consultant');

// Expect a title "to contain" a substring.
await expect(consultantName).toBeVisible()
test("has title", async ({ page }) => {
await page.goto("/");
const consultantName = page.getByText("Test Consultant");

await expect(consultantName).toBeVisible();
});

0 comments on commit 5eaa59c

Please sign in to comment.