|
| 1 | +import "reflect-metadata" |
| 2 | +import { |
| 3 | + createTestingConnections, |
| 4 | + closeTestingConnections, |
| 5 | + reloadTestingDatabases, |
| 6 | +} from "../../utils/test-utils" |
| 7 | +import { DataSource } from "../../../src" |
| 8 | +import { expect } from "chai" |
| 9 | + |
| 10 | +import { Category, Product } from "./entity" |
| 11 | + |
| 12 | +describe("github issues > #10431 When requesting nested relations on foreign key primary entities, relation becomes empty entity rather than null", () => { |
| 13 | + let connections: DataSource[] |
| 14 | + before( |
| 15 | + async () => |
| 16 | + (connections = await createTestingConnections({ |
| 17 | + entities: [Category, Product], |
| 18 | + schemaCreate: true, |
| 19 | + dropSchema: true, |
| 20 | + })), |
| 21 | + ) |
| 22 | + beforeEach(() => reloadTestingDatabases(connections)) |
| 23 | + after(() => closeTestingConnections(connections)) |
| 24 | + |
| 25 | + it("should return [] when requested nested relations are empty on ManyToMany relation with @VirtualColumn definitions", () => |
| 26 | + Promise.all( |
| 27 | + connections.map(async (connection) => { |
| 28 | + const productRepo = connection.getRepository(Product) |
| 29 | + const testProduct = new Product() |
| 30 | + testProduct.name = "foo" |
| 31 | + await productRepo.save(testProduct) |
| 32 | + const foundProduct = await productRepo.findOne({ |
| 33 | + where: { |
| 34 | + id: testProduct.id, |
| 35 | + }, |
| 36 | + relations: { categories: true }, |
| 37 | + }) |
| 38 | + expect(foundProduct?.name).eq("foo") |
| 39 | + expect(foundProduct?.categories).eql([]) |
| 40 | + }), |
| 41 | + )) |
| 42 | +}) |
0 commit comments