Skip to content

Commit

Permalink
feat: add tests for lib folder (#1961)
Browse files Browse the repository at this point in the history
  • Loading branch information
reachaadrika authored Jul 18, 2023
1 parent 66297e4 commit 039930a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cypress/test/lib/api.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getAllPosts, getPostBySlug, getDocBySlug } from '../../../lib/api';
import posts from '../../../config/posts.json'
describe('getAllPosts', () => {
it('should return all posts', () => {
const allPosts = getAllPosts();
expect(allPosts).to.deep.equal(posts);
});
});

describe('getPostBySlug', () => {
it('should return the post with the given slug', () => {
const slug = '/blog/2023-may-docs-report';
const post = getPostBySlug(slug);
const expectedPost = posts.blog.find((p) => p.slug === slug && !p.isSection);
expect(post).to.deep.equal(expectedPost);
});

it('should return the post of a specific type with the given slug', () => {
const slug = '/blog/2023-may-docs-report';
const type = 'blog';
const post = getPostBySlug(slug, type);
const expectedPost = posts[type].find((p) => p.slug === slug && !p.isSection);
expect(post).to.deep.equal(expectedPost);
});
});

describe('getDocBySlug', () => {
it('should return the document with the given slug', () => {
const structuredPosts = posts['docs']
const slug = '/docs/concepts';
const doc = getDocBySlug(structuredPosts, slug);
const expectedDoc = structuredPosts.find((post) => post.slug === slug && !post.isSection);
expect(doc).to.deep.equal(expectedDoc);
});

});

45 changes: 45 additions & 0 deletions cypress/test/lib/staticHelpers.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { getEvents } from '../../../lib/staticHelpers';

describe('getEvents', () => {
it('should return sorted events in ascending order', () => {
const events = [
{
"title": "Community Meeting",
"calLink": "https://www.google.com/calendar/event?eid=czRmMG5maHRsYjduM2g3dmwxMDM1Z3R0NzAgY19xOXRzZWlnbG9tZHNqNm5qdWh2YnB0czExY0Bn",
"url": "https://github.com/asyncapi/community/issues/645",
"date": "2023-04-04T08:00:00.000Z"
},
{
"title": "Spec 3.0 Meeting",
"calLink": "https://www.google.com/calendar/event?eid=djhsdjZvbmRsampvb2tsYzhkZWFyc3FtYTAgY19xOXRzZWlnbG9tZHNqNm5qdWh2YnB0czExY0Bn",
"url": "https://github.com/asyncapi/community/issues/649",
"date": "2023-04-13T15:00:00.000Z"
},
{
"title": "Community Meeting",
"calLink": "https://www.google.com/calendar/event?eid=MzgwdmZiMTc4cnBmbTUzdWVlbmM4aWYyM2MgY19xOXRzZWlnbG9tZHNqNm5qdWh2YnB0czExY0Bn",
"url": "https://github.com/asyncapi/community/issues/659",
"banner": "https://user-images.githubusercontent.com/40604284/229763606-c0b6ed3b-e120-427c-b87d-357856d92777.png",
"date": "2023-04-18T16:00:00.000Z"
},
];

cy.wrap(events).as('events');

// Call the getEvents function
cy.get('@events').then((events) => {
const sortedEvents = getEvents(events);

// Check if the result is an array
expect(sortedEvents).to.be.an('array');

// Convert dates to timestamps for comparison
const sortedTimestamps = sortedEvents.map((event) => event.date.valueOf());

// Check if the timestamps are sorted in ascending order
for (let i = 0; i < sortedTimestamps.length - 1; i++) {
expect(sortedTimestamps[i]).to.be.greaterThan(sortedTimestamps[i + 1]);
}
});
});
});

0 comments on commit 039930a

Please sign in to comment.