Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ayandajuqu committed Sep 18, 2024
1 parent 36f1e2d commit 43f1676
Show file tree
Hide file tree
Showing 8 changed files with 480 additions and 265 deletions.
479 changes: 259 additions & 220 deletions src/src/lib/components/annotations/3dAnnotations.svelte

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const load: PageServerLoad = async ({ params, locals }) => {
content: option.content,
points: option.points
}))
: null // if null
: null
})),
workspaceID,
models
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fail } from '@sveltejs/kit';

import { describe, it, expect, vi, beforeEach } from 'vitest';
//import mongoose from 'mongoose';

Expand Down Expand Up @@ -116,48 +116,5 @@ describe('Quizzes Management', () => {
expect(Quizzes.findByIdAndDelete).toHaveBeenCalledWith('123');
});
});

describe('actions.toggleAvailability', () => {
it('should toggle quiz availability successfully', async () => {
const mockFormData = new FormData();
mockFormData.append('quizId', '123');
mockFormData.append('isAvailable', 'true');

const mockRequest = {
formData: vi.fn().mockResolvedValue(mockFormData)
};

const mockQuiz = {
isAvailable: false,
save: vi.fn()
};

(Quizzes.findById as any).mockResolvedValue(mockQuiz);

const result = await quizzesModule.actions.toggleAvailability({
request: mockRequest
} as any);

expect(result).toEqual({ success: true });
expect(mockQuiz.isAvailable).toBe(true);
expect(mockQuiz.save).toHaveBeenCalled();
});

it('should fail if quiz not found', async () => {
const mockFormData = new FormData();
mockFormData.append('quizId', '123');
mockFormData.append('isAvailable', 'true');

const mockRequest = {
formData: vi.fn().mockResolvedValue(mockFormData)
};

(Quizzes.findById as any).mockResolvedValue(null);

await quizzesModule.actions.toggleAvailability({ request: mockRequest } as any);

expect(fail).toHaveBeenCalledWith(404, { error: 'Quiz not found' });
});
});
});
});
62 changes: 62 additions & 0 deletions src/src/tests/unit/components/modal/createQuiz.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, it, expect } from 'vitest';
import { render, fireEvent } from '@testing-library/svelte';
import ModalComponent from '$lib/components/modals/quizzes/Add.svelte';

describe('ModalComponent - Add Quiz Modal', () => {
const defaultProps = {
open: true
};

it('should open modal when open is true', () => {
const { getByText } = render(ModalComponent, {
props: { ...defaultProps, open: true }
});

expect(getByText('Add Quiz')).toBeInTheDocument();
});

it('should hide modal when open is false', () => {
const { queryByText } = render(ModalComponent, {
props: { ...defaultProps, open: false }
});

expect(queryByText('Add Quiz')).not.toBeInTheDocument();
});



it('should submit form with valid inputs', async () => {
const { getByLabelText, getByRole, getByText } = render(ModalComponent, {
props: { ...defaultProps }
});

const titleInput = getByLabelText('Title') as HTMLInputElement;
const durationInput = getByLabelText('Duration (in minutes)') as HTMLInputElement;
const instructionsTextarea = getByLabelText('Add Instructions') as HTMLTextAreaElement;
const submitButton = getByRole('button', { name: 'Create Quiz' });


await fireEvent.input(titleInput, { target: { value: 'New Quiz' } });
await fireEvent.input(durationInput, { target: { value: '45' } });
await fireEvent.input(instructionsTextarea, { target: { value: 'These are instructions.' } });

// Simulate form submission
await fireEvent.click(submitButton);

expect(titleInput.value).toBe('New Quiz');
expect(durationInput.value).toBe('45');
expect(instructionsTextarea.value).toBe('These are instructions.');
});

it('should display toolbar buttons correctly', () => {
const { getByRole } = render(ModalComponent, {
props: { ...defaultProps, open: true }
});

expect(getByRole('button', { name: 'Attach file' })).toBeInTheDocument();
expect(getByRole('button', { name: 'Embed map' })).toBeInTheDocument();
expect(getByRole('button', { name: 'Upload image' })).toBeInTheDocument();
expect(getByRole('button', { name: 'Format code' })).toBeInTheDocument();
expect(getByRole('button', { name: 'Add emoji' })).toBeInTheDocument();
});
});
67 changes: 67 additions & 0 deletions src/src/tests/unit/components/modal/questionType.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { describe, it, expect } from 'vitest';
import { render, fireEvent } from '@testing-library/svelte';
import ModalComponent from '$lib/components/modals/quizzes/Edit.svelte';

describe('ModalComponent - Question Type Modal', () => {
const defaultProps = {
open: true
};

it('should open modal when open is true', () => {
const { getByText } = render(ModalComponent, {
props: { ...defaultProps, open: true }
});

expect(getByText('Choose Question Type')).toBeInTheDocument();
});

it('should display "Add" button and disable it when no option is selected', () => {
const { getByText } = render(ModalComponent, {
props: { ...defaultProps, open: true }
});

const addButton = getByText('Add');
expect(addButton).toBeInTheDocument();
expect(addButton).toBeDisabled();
});

it('should enable "Add" button when a question type is selected', async () => {
const { getByText, getByLabelText } = render(ModalComponent, {
props: { ...defaultProps, open: true }
});

const selectInput = getByLabelText('Select Question Type');
const addButton = getByText('Add');

// Initially disabled
expect(addButton).toBeDisabled();

// Simulate selecting a question type
await fireEvent.change(selectInput, { target: { value: 'multiple-choice' } });

// Now the button should be enabled
expect(addButton).not.toBeDisabled();
});

it('should close modal when "Add" button is clicked with a valid selection', async () => {
const { getByText, getByLabelText, component } = render(ModalComponent, {
props: { ...defaultProps, open: true }
});

const selectInput = getByLabelText('Select Question Type');
const addButton = getByText('Add');

// Mock the dispatch event listener
let dispatchedEvent = null;
component.$on('select', (event) => {
dispatchedEvent = event.detail;
});

// Simulate selecting a question type and clicking the "Add" button
await fireEvent.change(selectInput, { target: { value: '3d-hotspot' } });
await fireEvent.click(addButton);

// Check if the event was dispatched correctly with the selected type
expect(dispatchedEvent).toEqual({ type: '3d-hotspot' });
});
});
45 changes: 45 additions & 0 deletions src/src/tests/unit/components/modal/submission.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, it, expect, vi } from 'vitest';
import { render, fireEvent } from '@testing-library/svelte';
import ModalComponent from '$lib/components/modals/quizzes/Submission.svelte'; // Adjust the import path as needed

// Mock the navigation function
vi.mock('$utils/navigation', () => ({
navigateToParentRoute: vi.fn()
}));

describe('ModalComponent - Quiz Submission Modal', () => {
const defaultProps = {
open: true,
submissionMessage: 'Your quiz has been submitted successfully!',
percentageScore: 85.5
};

it('should open modal when open is true', () => {
const { getByText } = render(ModalComponent, {
props: { ...defaultProps, open: true }
});

expect(getByText('Quiz Submission')).toBeInTheDocument();
});

it('should hide modal when open is false', () => {
const { queryByText } = render(ModalComponent, {
props: { ...defaultProps, open: false }
});

expect(queryByText('Quiz Submission')).not.toBeInTheDocument();
});



it('should display submission message and percentage score correctly', () => {
const { getByText } = render(ModalComponent, {
props: defaultProps
});

expect(getByText('Your quiz has been submitted successfully!')).toBeInTheDocument();
expect(getByText('Percentage Score: 85.50%')).toBeInTheDocument();
});


});
45 changes: 45 additions & 0 deletions src/src/tests/unit/components/modal/timeElasped.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, it, expect, vi } from 'vitest';
import { render, fireEvent } from '@testing-library/svelte';
import ModalComponent from '$lib/components/modals/quizzes/TimeElapsed.svelte'; // Adjust the import path as needed

// Mock the navigation function
vi.mock('$utils/navigation', () => ({
navigateToParentRoute: vi.fn()
}));

describe('ModalComponent - Time Elapsed Modal', () => {
const defaultProps = {
open: true,
submissionMessage: 'Time is up! You have completed the quiz.',
totalPoints: 85
};

it('should open modal when open is true', () => {
const { getByText } = render(ModalComponent, {
props: { ...defaultProps, open: true }
});

expect(getByText('Time Elapsed')).toBeInTheDocument();
});

it('should hide modal when open is false', () => {
const { queryByText } = render(ModalComponent, {
props: { ...defaultProps, open: false }
});

expect(queryByText('Time Elapsed')).not.toBeInTheDocument();
});



it('should display submission message and total points correctly', () => {
const { getByText } = render(ModalComponent, {
props: defaultProps
});

expect(getByText('Time is up! You have completed the quiz.')).toBeInTheDocument();
expect(getByText('Total Points: 85')).toBeInTheDocument();
});


});
Binary file added src/static/images/circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 43f1676

Please sign in to comment.