Skip to content

Commit

Permalink
Merge pull request #52 from jdufitum/testSuit
Browse files Browse the repository at this point in the history
Implemented a new test suite for new Implemented Features under path test/newImplementedFeature :the anonymous feature post, Instructor approval post and Estimanted reading Time
  • Loading branch information
jdufitum authored Oct 20, 2024
2 parents cb51105 + 3eddc7f commit 8c26d44
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 0 deletions.
97 changes: 97 additions & 0 deletions test/newImplementedFeatures/annonymousFeature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use strict';

const { JSDOM } = require('jsdom');
const { expect } = require('chai');

Check failure on line 4 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module 'chai'
const request = require('supertest');

Check failure on line 5 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module 'supertest'
const app = require('../../app');
const user = require('../../src/user');
const topics = require('../../src/topics');
const posts = require('../../src/posts');

describe('Anonymous Feature - Share Post Anonymously', () => {
let studentToken;
let postId;
let topicId;

before(async () => {
// Create a student user
const student = await user.create({ username: 'student', password: 'password' });

// Log in as student to get token
studentToken = await loginUser('student', 'password');

// Create a topic and a post
const topic = await topics.post({
title: 'Test Topic',
content: 'Test Content',
uid: student.uid,
cid: 1,
});
topicId = topic.tid;
const post = await posts.create({
content: 'Test Post',
uid: student.uid,
tid: topic.tid,
annonymousType: 'student',
});
postId = post.pid;
});

async function loginUser(username, password) {
const res = await request(app)
.post('/api/v1/login')
.send({ username, password });
return res.body.token;
}

describe('UI Elements', () => {
it('should display the anonymous type dropdown', () => {
const dom = new JSDOM(`
<div class="form-group">
<label for="anonymousDropdown">[[topic:composer.select-anonymous-type]]</label>
<select class="form-control" id="anonymousDropdown" name="anonymousType">
<option value="none">[[topic:composer-anonymous-none]]</option>
<option value="student">[[topic:composer-anonymous-student]]</option>
<option value="all">[[topic:composer-anonymous-all]]</option>
</select>
</div>
`);
const { document } = dom.window;
const dropdown = document.getElementById('anonymousDropdown');
expect(dropdown).to.not.be.null;

Check failure on line 61 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Expected an assignment or function call and instead saw an expression
expect(dropdown.options.length).to.equal(3);
});
});

describe('Form Submission', () => {
it('should include the anonymous type in the form submission', async () => {
const res = await request(app)
.post('/api/v1/topics')
.set('Authorization', `Bearer ${studentToken}`)
.send({
title: 'Anonymous Post',
content: 'This is an anonymous post.',
cid: 1,
annonymousType: 'student',
})
.expect(200);

expect(res.body.topic.annonymousType).to.equal('student');
});
});

describe('Backend Handling', () => {
it('should display the post anonymously based on the selected type', async () => {
const res = await request(app)
.get(`/api/v1/posts/${postId}`)
.expect(200);

const { post } = res.body;
if (post.annonymousType === 'none') {
expect(post.user.username).to.not.be.null;

Check failure on line 91 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Expected an assignment or function call and instead saw an expression
} else {
expect(post.user.username).to.be.null;

Check failure on line 93 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Expected an assignment or function call and instead saw an expression
}
});
});
});
45 changes: 45 additions & 0 deletions test/newImplementedFeatures/estimatedTimeForReadingPost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const { JSDOM } = require('jsdom');
const { expect } = require('chai');

Check failure on line 4 in test/newImplementedFeatures/estimatedTimeForReadingPost.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module 'chai'

describe('Estimated Time for Reading Post', () => {
let document;

beforeEach(() => {
const dom = new JSDOM(`
<div id="post-content-1">This is a test post content with a few words.</div>
<span id="reading-time-1" class="text-muted me-3"></span>
`);
document = dom.window.document;
});

it('should calculate the word count correctly', () => {
const postContentElement = document.getElementById('post-content-1');
const postContent = postContentElement.textContent;
const wordCount = postContent.trim().split(/\s+/).length;

expect(wordCount).to.equal(9); // Adjust the expected word count based on the content
});

it('should estimate the reading time correctly', () => {
const wordCount = 9; // Use the word count from the previous test
const wordsPerMinute = 200;
const readingTime = Math.ceil(wordCount / wordsPerMinute);

expect(readingTime).to.equal(1); // Adjust the expected reading time based on the word count
});

it('should display the estimated reading time in the DOM', () => {
const postContentElement = document.getElementById('post-content-1');
const readingTimeElement = document.getElementById('reading-time-1');

const postContent = postContentElement.textContent;
const wordCount = postContent.trim().split(/\s+/).length;
const readingTime = Math.ceil(wordCount / 200);

readingTimeElement.textContent = `Estimated reading time: ${readingTime} min`;

expect(readingTimeElement.textContent).to.equal('Estimated reading time: 1 min'); // Adjust the expected text based on the reading time
});
});
96 changes: 96 additions & 0 deletions test/newImplementedFeatures/instructorApproved.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
'use strict';

const assert = require('assert');
const request = require('supertest');

Check failure on line 4 in test/newImplementedFeatures/instructorApproved.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module 'supertest'
const app = require('../../app');
const db = require('../../src/database');
const user = require('../../src/user');
const topics = require('../../src/topics');
const posts = require('../../src/posts');
const categories = require('../../src/categories');

describe('Instructor Approved Posts', () => {
let instructorToken;
let studentToken;
let postId;
let topicId;

before(async () => {
// Create an instructor user and a student user
const instructor = await user.create({ username: 'instructor', password: 'password', isAdmin: true });
const student = await user.create({ username: 'student', password: 'password' });

// Log in as instructor and student to get tokens
instructorToken = await loginUser('instructor', 'password');
studentToken = await loginUser('student', 'password');

// Create a category, topic, and a post
const category = await categories.create({
name: 'Test Category',
description: 'Test category created by testing script',
});
const topic = await topics.post({
title: 'Test Topic',
content: 'Test Content',
uid: student.uid,
cid: category.cid,
});
topicId = topic.tid;
const post = await posts.create({
content: 'Test Post',
uid: student.uid,
tid: topic.tid,
});
postId = post.pid;
});

async function loginUser(username, password) {
const res = await request(app)
.post('/api/v1/login')
.send({ username, password });
return res.body.token;
}

describe('PUT /api/v1/posts/:pid/approve', () => {
it('should allow an instructor to approve a post', async () => {
const res = await request(app)
.put(`/api/v1/posts/${postId}/approve`)
.set('Authorization', `Bearer ${instructorToken}`)
.expect(200);

assert.strictEqual(res.body.message, 'Post approved successfully');

// Verify the post is marked as approved in the database
const post = await posts.getPost(postId);
assert.strictEqual(post.isApproved, true);
});

it('should not allow a student to approve a post', async () => {
const res = await request(app)
.put(`/api/v1/posts/${postId}/approve`)
.set('Authorization', `Bearer ${studentToken}`)
.expect(403);

assert.strictEqual(res.body.error, 'You do not have permission to approve posts');
});

it('should return an error if the post does not exist', async () => {
const res = await request(app)
.put('/api/v1/posts/99999/approve')
.set('Authorization', `Bearer ${instructorToken}`)
.expect(404);

assert.strictEqual(res.body.error, 'Post not found');
});
});

describe('GET /api/v1/posts/:pid', () => {
it('should retrieve the post with approval status', async () => {
const res = await request(app)
.get(`/api/v1/posts/${postId}`)
.expect(200);

assert.strictEqual(res.body.post.isApproved, true);
});
});
});

0 comments on commit 8c26d44

Please sign in to comment.