Skip to content

Commit

Permalink
Attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
TueeNguyen committed Apr 20, 2022
1 parent deade17 commit 5a257c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
17 changes: 8 additions & 9 deletions src/api/parser/src/utils/__mocks__/supabase.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
let feeds = [];

let feedIds = new Set();
module.exports = {
__setMockFeeds: (redisFeeds) => {
const mockFeeds = redisFeeds.map((feed) => {
return {
id: feed.id,
flagged: false,
};
});
feeds = mockFeeds;
const mockFeeds = redisFeeds
.filter((feed) => !feedIds.has(feed.id))
.map((feed) => ({ id: feed.id, flagged: false }));
feeds = feeds.concat(mockFeeds);
console.log(feeds);
},
// Flagged feed related functions
getAllFeeds: jest.fn().mockImplementation(() => Promise.resolve(feeds)),
Expand All @@ -35,7 +33,8 @@ module.exports = {
}),
isFlagged: jest.fn().mockImplementation((id) => {
const targetFeed = feeds.filter((feed) => feed.id === id)[0];

console.log(targetFeed);
console.log(feeds);
return Promise.resolve(targetFeed.flagged);
}),
};
13 changes: 10 additions & 3 deletions src/api/parser/test/feed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { search } = require('../src/utils/indexer');
const urlToId = (url) => hash(normalizeUrl(url));

jest.mock('../src/utils/supabase');
const { __setMockFeeds } = require('../src/utils/supabase');
const { __setMockFeeds } = require('../src/utils/__mocks__/supabase');

describe('Post data class tests', () => {
const data = {
Expand Down Expand Up @@ -95,7 +95,11 @@ describe('Post data class tests', () => {

describe('Get and Set Feed objects from database', () => {
const { mock } = Elastic();
beforeAll(() => createFeed().save());
beforeAll(async () => {
const feed = new Feed(data.author, data.url, data.user, data.link, null, null);
__setMockFeeds([feed]);
await feed.save();
});
afterEach(() => mock.clearAll());

test('Feed.byId() for a valid id should return a Feed', async () => {
Expand Down Expand Up @@ -131,6 +135,7 @@ describe('Post data class tests', () => {
test('Setting etag on a feed should persist', async () => {
const feed = new Feed('author', 'http://url.com/with/etag');
feed.etag = 'etag';
__setMockFeeds([feed]);
await feed.save();
const persisted = await Feed.byId(feed.id);
expect(persisted.etag).toEqual('etag');
Expand All @@ -142,6 +147,7 @@ describe('Post data class tests', () => {
test('Setting lastModified on a feed should persist', async () => {
const feed = new Feed('author', 'http://url.com/with/lastModified');
feed.lastModified = 'lastModified';
__setMockFeeds([feed]);
await feed.save();
const persisted = await Feed.byId(feed.id);
expect(persisted.lastModified).toEqual('lastModified');
Expand All @@ -161,6 +167,7 @@ describe('Post data class tests', () => {
const feed = new Feed(data.author, data.url, data.user, data.link, null, null);
feed.author = 'Author Post';
feed.url = 'https://modified.user.feed.com/feed.rss';
__setMockFeeds([feed]);
await feed.update();
const modifiedFeed = await Feed.byId(feed.id);
// modifiedFeed.id should be different from feed.id as feed hasn't been reassigned to updated feed.id value
Expand Down Expand Up @@ -239,7 +246,7 @@ describe('Post data class tests', () => {
let feed = new Feed(data.author, data.url, data.user, data.link, 'etag', 'lastModified');
let feed2 = new Feed(data2.author, data2.url, data2.user, data2.link, 'etag', 'lastModified');
let feed3 = new Feed(data3.author, data3.url, data3.user, data3.link, 'etag', 'lastModified');

__setMockFeeds([feed, feed2, feed3]);
feed = await Feed.byId(await Feed.create(feed));
feed2 = await Feed.byId(await Feed.create(feed2));
feed3 = await Feed.byId(await Feed.create(feed3));
Expand Down

0 comments on commit 5a257c9

Please sign in to comment.