Skip to content

Commit

Permalink
test: search users + assignments uses cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdonado committed Dec 13, 2023
1 parent 2a00a54 commit 5f4340b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 8 deletions.
70 changes: 64 additions & 6 deletions tests/pages/assignments.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { faker } from '@faker-js/faker';
import { eq, ne } from 'drizzle-orm';

import type { Locator } from '@playwright/test';
import { test, expect } from '@playwright/test';
Expand All @@ -10,17 +11,17 @@ import {
getAppSession,
} from '../helpers';

import type { AppSession } from '~/schemas/session';
import { assignmentsTable, usersTable } from '~/db/schema';
import { db } from '~/db/config.server';
import { eq, ne } from 'drizzle-orm';
import type { Assignment, AssignmentSerialized } from '~/schemas/assignment';
import { assignmentsTable, usersTable } from '~/db/schema';
import type { AppSession } from '~/schemas/session';
import type { AssignmentSerialized } from '~/schemas/assignment';
import { PAGE_SIZE } from '~/config/constants.server';

test.describe('Assignments page - Admin', () => {
test.use({ storageState: ADMIN_STORAGE_STATE });

const ASSIGNMENT_ROW = 1;
const TABLE_ROWS_LENGTH = 11;
const TABLE_ROWS_LENGTH = PAGE_SIZE + 1;

test.beforeEach(async ({ page }) => {
await page.goto('/assignments');
Expand Down Expand Up @@ -64,6 +65,63 @@ test.describe('Assignments page - Admin', () => {
});
});

test.describe('Search Assignments', () => {
let title: string | null;
let content: string | null;
let authorName: string | null;

test.beforeAll(async () => {
[{ title, content, authorName }] = await db
.select({
title: assignmentsTable.title,
content: assignmentsTable.content,
authorName: usersTable.name,
})
.from(assignmentsTable)
.leftJoin(usersTable, eq(assignmentsTable.authorId, usersTable.id))
.offset(PAGE_SIZE * 2)
.limit(1);
});

test('should filter by title', async ({ page }) => {
const searchBar = page.getByPlaceholder('Search assignments...');

await searchBar.fill(title!);
await searchBar.press('Enter');

await expect(page.getByRole('row')).toHaveCount(2);

const assignment = page.getByRole('row').nth(1);
await expect(assignment.getByRole('cell').first()).toHaveText(title!);
});

test('should filter by content', async ({ page }) => {
const searchBar = page.getByPlaceholder('Search assignments...');

await searchBar.fill(content!.slice(0, 10));
await searchBar.press('Enter');

await expect(page.getByRole('row')).toHaveCount(2);
const assignment = page.getByRole('row').nth(1);
await expect(assignment.getByRole('cell').nth(2).locator('p')).toHaveText(
new RegExp(`${content!.slice(0, 10)}`)
);
});

test('should filter by authorName', async ({ page }) => {
const searchBar = page.getByPlaceholder('Search assignments...');

await searchBar.fill(authorName!);
await searchBar.press('Enter');

await expect(page.getByRole('row')).toHaveCount(PAGE_SIZE + 1);
const assignment = page.getByRole('row').nth(1);
await expect(assignment.getByRole('cell').nth(1).locator('p')).toHaveText(
authorName!
);
});
});

test.describe('Show Assignment', () => {
test('should go to show assignment page', async ({ page }) => {
const assignment = page.getByRole('row').nth(ASSIGNMENT_ROW);
Expand Down Expand Up @@ -280,7 +338,7 @@ test.describe('Assignments page - Teacher', () => {
test.use({ storageState: TEACHER_STORAGE_STATE });

const ASSIGNMENT_ROW = 1;
const TABLE_ROWS_LENGTH = 11;
const TABLE_ROWS_LENGTH = PAGE_SIZE + 1;

test.beforeEach(async ({ page }) => {
await page.goto('/assignments');
Expand Down
45 changes: 43 additions & 2 deletions tests/pages/users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import {
VALID_PASSWORD,
} from '../helpers';

import { db } from '~/db/config.server';
import { usersTable } from '~/db/schema';

import { PAGE_SIZE } from '~/config/constants.server';

test.describe('Users page - Admin', () => {
test.use({ storageState: ADMIN_STORAGE_STATE });

const USER_ROW = 1;
const TABLE_ROWS_LENGTH = 11;
const TABLE_ROWS_LENGTH = PAGE_SIZE + 1;

test.beforeEach(async ({ page }) => {
await page.goto('/users');
Expand Down Expand Up @@ -57,6 +62,42 @@ test.describe('Users page - Admin', () => {
});
});

test.describe('Search Users', () => {
let name: string | null;
let username: string | null;

test.beforeAll(async () => {
[{ name, username }] = await db
.select({ name: usersTable.name, username: usersTable.username })
.from(usersTable)
.offset(PAGE_SIZE * 2)
.limit(1);
});

test('should filter by name', async ({ page }) => {
const searchBar = page.getByPlaceholder('Search users...');

await searchBar.fill(name!);
await searchBar.press('Enter');

await expect(page.getByRole('row')).toHaveCount(2);

const user = page.getByRole('row').nth(1);
await expect(user.getByRole('cell').first().locator('p')).toHaveText(name!);
});

test('should filter by username', async ({ page }) => {
const searchBar = page.getByPlaceholder('Search users...');

await searchBar.fill(username!);
await searchBar.press('Enter');

await expect(page.getByRole('row')).toHaveCount(2);
const user = page.getByRole('row').nth(1);
await expect(user.getByRole('cell').nth(1)).toHaveText(username!);
});
});

test.describe('Create User', () => {
test.beforeEach(async ({ page }) => {
const createUserButton = page.getByRole('link', { name: 'Create User' });
Expand Down Expand Up @@ -163,7 +204,7 @@ test.describe('Users page - Admin', () => {
await submitButton.click();
});

test('should edit Username HERE', async ({ page }) => {
test('should edit Username', async ({ page }) => {
const newUsername = 'New Username';

const usernameInput = page.getByLabel('Username');
Expand Down

0 comments on commit 5f4340b

Please sign in to comment.