Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vorant94 committed Jun 7, 2024
1 parent b82b881 commit 1a93f97
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 21 deletions.
7 changes: 7 additions & 0 deletions e2e/dashboard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from '@playwright/test';
import { route } from '../src/router/types/route';

test('should redirect from root to dashboard', async ({ page }) => {
await page.goto(route.root);
await expect(page).toHaveURL(route.dashboard);
});
14 changes: 0 additions & 14 deletions e2e/example.spec.ts

This file was deleted.

23 changes: 23 additions & 0 deletions e2e/subscriptions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect, test } from '@playwright/test';
import { route } from '../src/router/types/route';
import {
addSubscriptionButton,
noSubscriptionsPlaceholder,
subscriptionUpsertForm,
} from '../src/subscriptions/globals/subscription.test-id';

test('should create subscription', async ({ page }) => {
await page.goto(route.subscriptions);

await expect(
page.getByTestId(noSubscriptionsPlaceholder),
`should have no subscriptions initially`,
).toBeVisible();

await page.getByTestId(addSubscriptionButton).click();

await expect(
page.getByTestId(subscriptionUpsertForm),
`should show subscription upsert form`,
).toBeVisible();
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"start:dev": "vite",
"start:prod": "vite preview",
"build": "tsc && vite build",
"preview": "vite preview",
"format:check": "prettier . --check",
"format:fix": "prettier . --write",
"lint:check": "eslint .",
Expand Down
7 changes: 4 additions & 3 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { App } from '@/App.tsx';
import { DashboardPage } from '@/dashboard/pages/dashboard.page.tsx';
import { route } from '@/router/types/route.ts';
import { SubscriptionsPage } from '@/subscriptions/pages/subscriptions.page.tsx';
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
Expand All @@ -16,7 +17,7 @@ const router = createBrowserRouter([
Component: App,
children: [
{
path: '/',
path: route.root,
element: (
<Navigate
to="/dashboard"
Expand All @@ -25,11 +26,11 @@ const router = createBrowserRouter([
),
},
{
path: '/dashboard',
path: route.dashboard,
Component: DashboardPage,
},
{
path: '/subscriptions',
path: route.subscriptions,
Component: SubscriptionsPage,
},
],
Expand Down
7 changes: 7 additions & 0 deletions src/router/types/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const route = {
root: '/',
dashboard: '/dashboard',
subscriptions: '/subscriptions',
} as const;

export type Route = (typeof route)[keyof typeof route];
9 changes: 8 additions & 1 deletion src/subscriptions/components/add-subscription-button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { memo, useCallback, useContext } from 'react';
import { addSubscriptionButton } from '../globals/subscription.test-id.ts';
import { SubscriptionUpsertStateContext } from './subscription-upsert.tsx';

export const AddSubscriptionButton = memo(() => {
Expand All @@ -9,5 +10,11 @@ export const AddSubscriptionButton = memo(() => {
[upsert],
);

return <button onClick={onClick}>add sub</button>;
return (
<button
data-testid={addSubscriptionButton}
onClick={onClick}>
add sub
</button>
);
});
3 changes: 2 additions & 1 deletion src/subscriptions/components/subscription-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SplitLayoutContext } from '@/ui/layouts/split.layout.tsx';
import { cn } from '@/ui/utils/cn.ts';
import { useLiveQuery } from 'dexie-react-hooks';
import { memo, useContext } from 'react';
import { noSubscriptionsPlaceholder } from '../globals/subscription.test-id.ts';
import { findSubscriptions } from '../models/subscription.table.ts';
import { SubscriptionListItem } from './subscription-list-item.tsx';

Expand All @@ -23,7 +24,7 @@ export const SubscriptionList = memo(() => {
/>
))
) : (
<div>No Subscriptions</div>
<div data-testid={noSubscriptionsPlaceholder}>No Subscriptions</div>
)}
</div>
);
Expand Down
2 changes: 2 additions & 0 deletions src/subscriptions/components/subscription-upsert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from 'react';
import { useForm, type SubmitHandler } from 'react-hook-form';
import { usePrevious } from 'react-use';
import { subscriptionUpsertForm } from '../globals/subscription.test-id.ts';
import {
type InsertSubscriptionModel,
type SubscriptionModel,
Expand Down Expand Up @@ -81,6 +82,7 @@ export const SubscriptionUpsert = memo(() => {
<h1>{`${state.mode === 'update' ? 'Update' : 'Insert'} Subscription`}</h1>

<form
data-testid={subscriptionUpsertForm}
onSubmit={handleSubmit(onSubmit)}
className={cn('flex flex-col gap-2 self-stretch')}>
<input
Expand Down
4 changes: 4 additions & 0 deletions src/subscriptions/globals/subscription.test-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const noSubscriptionsPlaceholder =
'no-subscriptions-placeholder' as const;
export const addSubscriptionButton = 'add-subscription-button' as const;
export const subscriptionUpsertForm = 'subscription-upsert-form' as const;

0 comments on commit 1a93f97

Please sign in to comment.