Skip to content

Commit

Permalink
feat(core): allow adding items to not initialized collections (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Aug 9, 2020
1 parent 77c52dd commit 8be8a4d
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/EntityManager.mysql.test.ts
Expand Up @@ -1908,6 +1908,56 @@ describe('EntityManagerMySql', () => {
expect(() => orm.em.remove(Book2, null)).toThrowError(`You cannot call 'EntityManager.remove()' with empty 'where' parameter. If you want to remove all entities, use 'em.remove(Book2, {})'.`);
});

test('adding items to not initialized collection', async () => {
const god = new Author2('God', 'hello@heaven.god');
const b1 = new Book2('Bible 1', god);
await orm.em.persistAndFlush(b1);
orm.em.clear();

const a = await orm.em.findOneOrFail(Author2, god.id);
expect(a.books.isInitialized()).toBe(false);
const b2 = new Book2('Bible 2', a);
const b3 = new Book2('Bible 3', a);
a.books.add(b2, b3);
await orm.em.flush();
orm.em.clear();

const a2 = await orm.em.findOneOrFail(Author2, god.id, ['books']);
expect(a2.books.count()).toBe(3);
expect(a2.books.getIdentifiers()).toEqual([b1.uuid, b2.uuid, b3.uuid]);

const tag1 = new BookTag2('silly');
const tag2 = new BookTag2('funny');
const tag3 = new BookTag2('sick');
const tag4 = new BookTag2('strange');
let tag5 = new BookTag2('sexy');
a2.books[0].tags.add(tag1);
a2.books[1].tags.add(tag1);
a2.books[2].tags.add(tag5);
await orm.em.flush();
orm.em.clear();

const a3 = await orm.em.findOneOrFail(Author2, god.id, ['books']);
tag5 = orm.em.getReference(BookTag2, tag5.id);
a3.books[0].tags.add(tag3);
a3.books[1].tags.add(tag2, tag5);
a3.books[2].tags.add(tag4);
await orm.em.flush();
orm.em.clear();

const a4 = await orm.em.findOneOrFail(Author2, god.id, ['books.tags']);
expect(a4.books[0].tags.getIdentifiers()).toEqual([tag1.id, tag3.id]);
expect(a4.books[1].tags.getIdentifiers()).toEqual([tag1.id, tag2.id, tag5.id]);
expect(a4.books[2].tags.getIdentifiers()).toEqual([tag5.id, tag4.id]);
orm.em.clear();

const tag = await orm.em.findOneOrFail(BookTag2, tag1.id);
const book = await orm.em.findOneOrFail(Book2, b3.uuid);
tag.books.add(book);
await orm.em.flush();
orm.em.clear();
});

afterAll(async () => orm.close(true));

});

0 comments on commit 8be8a4d

Please sign in to comment.