Unit tests for Post

This commit is contained in:
Chegele 2023-07-06 15:46:35 -04:00
parent 81303586d7
commit 42e2d05177
1 changed files with 162 additions and 0 deletions

View File

@ -0,0 +1,162 @@
import { Vertex } from "../graph.interface";
import { Citation } from "./citation";
import { Member } from "./member";
import { Post } from "./post";
describe('Reputation Service Post', () => {
let testingPostA: Post;
let testingPostB: Post;
let testingCitation: Citation;
const TEST_AUTHOR_A = new Member("testAuthorA");
const TEST_TITLE_A = "The Hitchhiker's Guide to the Galaxy";
const TEST_CONTENT_A = "The ships hung in the sky in much the same way that bricks don't.";
const TEST_AUTHOR_B = new Member("testAuthorB");
const TEST_TITLE_B = "The Restaurant at the End of the Universe";
const TEST_CONTENT_B = "The story so far: In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move.";
const SERIALIZED = `{"id":"testingPostA","title":"The Hitchhiker's Guide to the Galaxy","content":"The ships hung in the sky in much the same way that bricks don't.","authors":[{"author":"testAuthorA","weight":1000}],"citations":[{"citation":"testingCitation","weight":300}]}`;
beforeEach(() => {
testingPostA = new Post("testingPostA", TEST_TITLE_A, TEST_CONTENT_A, TEST_AUTHOR_A);
testingPostB = new Post("testingPostB", TEST_TITLE_B, TEST_CONTENT_B, TEST_AUTHOR_B);
testingCitation = new Citation("testingCitation", testingPostA, testingPostB, 10);
testingPostA.setCitation(testingCitation, 300);
});
it('should be able to get all posts', () => {
const posts = Post.getAllPosts();
expect(posts.length).toBe(2);
expect(posts[0].id).toBe("testingPostA");
expect(posts[1].id).toBe("testingPostB");
});
it('should be able to get a specific post', () => {
const post = Post.getPost("testingPostA");
expect(post).toBeDefined();
expect(post?.title).toBe(TEST_TITLE_A);
});
it('should be able to create a new post with random id', () => {
const post = new Post(null, TEST_TITLE_A, TEST_CONTENT_A, TEST_AUTHOR_A);
expect(typeof post.id).toBe("string");
expect(post.id.length).toBeGreaterThan(5);
});
it('should be able to create a new post with specific id', () => {
const post = new Post("testing", TEST_TITLE_A, TEST_CONTENT_A, TEST_AUTHOR_A);
expect(post.id).toBe("testing");
});
it('should be able to get the id of a post', () => {
expect(testingPostA.id).toBe("testingPostA");
});
it('should be able to get the edges from Post as a Vertex', () => {
const vertex = testingPostA as Vertex;
expect(vertex.edges.length).toBe(1);
});
it('should be able to get the data from Post as a Vertex', () => {
const vertex = testingPostA as Vertex;
const data = vertex.data;
expect(vertex.data).toBeDefined();
expect(typeof data).toBe("string");
});
it('should be able to get the title of a post', () => {
expect(testingPostA.title).toBe(TEST_TITLE_A);
});
it('should be able to get the content of a post', () => {
expect(testingPostA.content).toBe(TEST_CONTENT_A);
});
it('should be able to get the authors of a post', () => {
expect(testingPostA.authors.length).toBe(1);
expect(testingPostA.authors[0]).toBe(TEST_AUTHOR_A);
});
it('should be able to get the citations of a post', () => {
expect(testingPostA.citations.length).toBe(1);
expect(testingPostA.citations[0]).toBe(testingCitation);
});
it('should be able to get the total authors weight of a post', () => {
expect(testingPostA.authorsWeightTotal).toBe(1000);
});
it('should be able to get the total citations weight of a post', () => {
expect(testingPostA.citationsWeightTotal).toBe(300);
});
it('should be able to adjust an authors weight within a post', () => {
testingPostA.setAuthor(TEST_AUTHOR_A, 10);
expect(testingPostA.authorsWeightTotal).toBe(10);
});
it('should be able to add multiple authors to a post', () => {
testingPostA.setAuthor(TEST_AUTHOR_B, 500);
expect(testingPostA.authors.length).toBe(2);
expect(testingPostA.authors[1]).toBe(TEST_AUTHOR_B);
expect(testingPostA.authorsWeightTotal).toBe(1500);
});
it('should be able to remove an author from a post', () => {
testingPostA.setAuthor(TEST_AUTHOR_B, 500);
testingPostA.removeAuthor(TEST_AUTHOR_B);
expect(testingPostA.authors.length).toBe(1);
expect(testingPostA.authors[0]).toBe(TEST_AUTHOR_A);
expect(testingPostA.authorsWeightTotal).toBe(1000);
});
it('should not be able to remove all authors from a post', () => {
expect(() => {
testingPostA.removeAuthor(TEST_AUTHOR_A);
}).toThrowError("Cannot remove the last author of a post.");
});
it('should be able to add citations to a post', () => {
const citation = new Citation("testingCitation", testingPostB, testingPostA, 100);
testingPostA.setCitation(citation, 100);
expect(testingPostA.citations.length).toBe(2);
expect(testingPostA.citations[1]).toBe(citation);
expect(testingPostA.citationsWeightTotal).toBe(400);
});
it('should be able to remove citations from a post', () => {
testingPostA.removeCitation(testingCitation);
expect(testingPostA.citations.length).toBe(0);
expect(testingPostA.citationsWeightTotal).toBe(0);
});
it('should be able to get the weight of an author within a post', () => {
expect(testingPostA.getWeight(TEST_AUTHOR_A)).toBe(1000);
});
it('should be able to get the weight of a citation within a post', () => {
expect(testingPostA.getWeight(testingCitation)).toBe(300);
});
it('should be able to serialize a post', () => {
const serialized = Post.serialize(testingPostA, false);
expect(serialized).toBe(SERIALIZED);
});
it('should be able to parse a post', () => {
const post = Post.parse(SERIALIZED);
expect(post).toBeDefined();
expect(post?.id).toBe("testingPostA");
expect(post?.title).toBe(TEST_TITLE_A);
expect(post?.content).toBe(TEST_CONTENT_A);
expect(post?.authors.length).toBe(1);
expect(post?.authors[0]).toBe(TEST_AUTHOR_A);
expect(post?.authorsWeightTotal).toBe(1000);
expect(post?.citations.length).toBe(1);
expect(post?.citations[0]).toBe(testingCitation);
expect(post?.citationsWeightTotal).toBe(300);
});
});