import { MikroORM } from '@mikro-orm/mongodb';

import { Author, Book } from '../entities';
import { closeReplSets, initORMMongo } from '../bootstrap';

describe('GH4065', () => {

  let orm: MikroORM;

  beforeAll(async () => orm = await initORMMongo(true));
  beforeEach(async () => orm.schema.clearDatabase());

  afterAll(async () => {
    await orm.close();
    await closeReplSets();
  });

  test('should load entities with $fulltext and filter set', async () => {
    const god = new Author('God', 'hello@heaven.god');
    const bible = new Book('Bible', god);
    await orm.em.persistAndFlush(bible);
    orm.em.clear();

    const booksRepository = orm.em.getRepository(Book);
    orm.em.addFilter('BreakFulltext', { title: { $re: '.*' } }, Book, true);
    const books = await booksRepository.find({ $fulltext: 'Bible' });
    expect(books.length).toBe(1);
  });
});
