Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Unrelated collection elements are deleted when searching collections cache on deletion. #343

Open
wants to merge 1 commit into
base: v2.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { SourceType } from './document';
import { IDocumentData } from './interfaces/document';
import { DocumentCollection } from './document-collection';
import { ClonedResource } from './cloned-resource';
import arrayContaining = jasmine.arrayContaining;

// @todo disable PhotoService
// @TODO: fix error in toObject when relationship's service is not injected
Expand Down Expand Up @@ -1318,3 +1319,43 @@ describe('service.get()', () => {
expect(book_clone.relationships.author.loaded).toBe(original_book.relationships.author.loaded);
});
});

describe('service.delete()', () => {
let core: Core;
let authorsService: AuthorsService;
let booksService: BooksService;
beforeEach(async () => {
core = new Core(new JsonapiConfig(), new JsonapiHttpImported(new HttpClient(new HttpHandlerMock()), new JsonapiConfig()), injector);
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]));
});
});
11 changes: 6 additions & 5 deletions src/services/cachememory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ export class CacheMemory<R extends Resource = Resource> {
return;
}
Base.forEach(this.collections, (value, url) => {
value.data.splice(
value.data.findIndex(
(resource_on_collection: Resource) => resource_on_collection.type === type && resource_on_collection.id === id
),
1
const idx = value.data.findIndex(
(resource_on_collection: Resource) => resource_on_collection.type === type && resource_on_collection.id === id
);

if (idx !== -1) {
value.data.splice(idx, 1);
}
});
resource.attributes = {}; // just for confirm deletion on view

Expand Down