reyesoft / ngx-jsonapi

JSON API client library for Angular 5+ 👌 :: Production Ready 🚀
https://ngx-jsonapi.reyesoft.com/
MIT License
101 stars 52 forks source link

Bug: service.delete() for relationships removes unrelated entities from the result of parent's service.all() #262

Open ivelichko opened 4 years ago

ivelichko commented 4 years ago

Let's have an Author entity which has one-to-many relationship with Book entity. Let's have Author1 with Book1 and Book2, and Author2.

let authors = await authorsService.all({ include: ['books'] }).toPromise(); // will return Author1 with Book1 and Author2
console.log(authors.data); // will show both Author1 and Author2
await booksService.delete('Book1').toPromise(); // will delete Book1
console.log(authors.data); // will show only Author1, Author2 is missing!!!

My guess is that something is wrong with CacheMemory. The bug is reproducible at least on 2.1.12 and later (I haven't tried earlier versions).


I created a failing test case for service.spec.ts:


describe('service.delete()', () => {
    let core: Core;
    let authorsService: AuthorsService;
    let booksService: BooksService;
    beforeEach(async () => {
        core = new Core(
            new JsonapiConfig(),
            new JsonapiStore(),
            new JsonapiHttpImported(new HttpClient(new HttpHandlerMock()), new JsonapiConfig())
        );
        authorsService = new AuthorsService();
        authorsService.register();
        booksService = new BooksService();
        booksService.register();
        await authorsService.clearCache();
        await booksService.clearCache();
        test_response_subject.complete();
        test_response_subject = new BehaviorSubject(new HttpResponse());
    });

    it(`.delete() for relationship does not remove entities from parent's .all()`, async () => {
        // given
        test_response_subject.next(new HttpResponse({ body: TestFactory.getCollectionDocumentData(Author) }));
        let authors = await authorsService.all({ include: ['books'] }).toPromise();
        test_response_subject.complete();

        let author1 = authors.data[0];
        let book = author1.relationships.books.data[0];
        let author2 = authors.data[1];

        expect(author2).toBeTruthy();
        expect(authors.data).toEqual(arrayContaining([author1, author2]));

        // when
        test_response_subject = new BehaviorSubject(new HttpResponse());
        test_response_subject.next(new HttpResponse());
        await booksService.delete(book.id).toPromise();
        test_response_subject.complete();

        // then
        expect(authors.data).toEqual(arrayContaining([author1, author2]));
    });
});
ivelichko commented 3 years ago

any updates?

iruizr7 commented 2 years ago

I am hitting this too.