Skip to content

Commit

Permalink
Adding more tests for flyers and magazines
Browse files Browse the repository at this point in the history
  • Loading branch information
Aayush-Agnihotri committed Nov 21, 2023
1 parent 496e839 commit cdd926a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ export class User {
bookmarkedArticles: mongoose.Types.DocumentArray<DocumentType<Article>>;

@Field((type) => [Magazine])
@Property({ required: true, type: () => String, default: [] })
@Property({ required: true, type: () => Magazine, default: [] })
bookmarkedMagazines: mongoose.Types.DocumentArray<DocumentType<Magazine>>;

@Field((type) => [Flyer])
@Property({ required: true, type: () => String, default: [] })
@Property({ required: true, type: () => Flyer, default: [] })
bookmarkedFlyers: mongoose.Types.DocumentArray<DocumentType<Flyer>>;

@Field({ nullable: true })
Expand Down
6 changes: 3 additions & 3 deletions src/repos/MagazineRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ const getMagazinesByPublicationSlugs = async (
});
};

function getMagazineByID(id: string) {
return MagazineModel.findById(id).then((magazine) => {
const getMagazineByID = async (id: string): Promise<Magazine> => {
return MagazineModel.findById(new ObjectId(id)).then((magazine) => {
if (!isMagazineFiltered(magazine)) {
return magazine;
}
return null;
});
}
};

function getMagazinesByIDs(ids: string[]) {
return Promise.all(ids.map((id) => MagazineModel.findById(new ObjectId(id)))).then(
Expand Down
32 changes: 25 additions & 7 deletions src/tests/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,15 @@ describe('(un)bookmark tests:', () => {
await UserRepo.bookmarkArticle(users[0].uuid, insertOutput[0].id);

// update database
const pub = await PublicationFactory.getRandomPublication();
await UserRepo.followPublication(users[0].uuid, pub.slug);
const pub1 = await PublicationFactory.getRandomPublication();
await UserRepo.followPublication(users[0].uuid, pub1.slug);

await UserRepo.unbookmarkArticle(users[0].uuid, insertOutput[0].id);

// update database
const pub2 = await PublicationFactory.getRandomPublication();
await UserRepo.followPublication(users[0].uuid, pub2.slug);

const getUserResponse = await UserRepo.getUserByUUID(users[0].uuid);
expect(getUserResponse.bookmarkedArticles).toHaveLength(0);
});
Expand All @@ -251,10 +256,19 @@ describe('(un)bookmark tests:', () => {
const users = await UserFactory.create(1);
const articles = await ArticleFactory.create(1);
await UserModel.insertMany(users);
await ArticleModel.insertMany(articles);
const insertOutput = await ArticleModel.insertMany(articles);
await UserRepo.bookmarkArticle(users[0].uuid, insertOutput[0].id);

// update database
const pub1 = await PublicationFactory.getRandomPublication();
await UserRepo.followPublication(users[0].uuid, pub1.slug);

await UserRepo.bookmarkArticle(users[0].uuid, insertOutput[0].id);

// update database
const pub2 = await PublicationFactory.getRandomPublication();
await UserRepo.followPublication(users[0].uuid, pub2.slug);

await UserRepo.bookmarkArticle(users[0].uuid, articles[0].id);
await UserRepo.bookmarkArticle(users[0].uuid, articles[0].id);
const getUserResponse = await UserRepo.getUserByUUID(users[0].uuid);
expect(getUserResponse.bookmarkedArticles).toHaveLength(1);
});
Expand All @@ -263,9 +277,13 @@ describe('(un)bookmark tests:', () => {
const users = await UserFactory.create(1);
const articles = await ArticleFactory.create(1);
await UserModel.insertMany(users);
await ArticleModel.insertMany(articles);
const insertOutput = await ArticleModel.insertMany(articles);

await UserRepo.unbookmarkArticle(users[0].uuid, articles[0].id);
// update database
const pub = await PublicationFactory.getRandomPublication();
await UserRepo.followPublication(users[0].uuid, pub.slug);

await UserRepo.unbookmarkArticle(users[0].uuid, insertOutput[0].id);
const getUserResponse = await UserRepo.getUserByUUID(users[0].uuid);
expect(getUserResponse.bookmarkedArticles).toHaveLength(0);
});
Expand Down

0 comments on commit cdd926a

Please sign in to comment.