Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
feat(core): added reset for state stack and test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasdik1989 committed Oct 10, 2019
1 parent babddef commit 72d4a79
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
34 changes: 30 additions & 4 deletions packages/core/__tests__/Resource/Resource.state.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ describe('The Resource state', () => {

test('Test that serialization of a resource state works', () => {
const authorImage = Resource.create(5, 'authorImage', { src: 'image.jpg' });
const publication1 = Resource.create(3, 'publication', { name: 'Publication 1'});
const publication2 = Resource.create(4, 'publication', { name: 'Publication 2'});
const publication1 = Resource.create(3, 'publication', { name: 'Publication 1' });
const publication2 = Resource.create(4, 'publication', { name: 'Publication 2' });

const author = Resource.create(1, 'author', {
name: 'John',
Expand Down Expand Up @@ -92,11 +92,37 @@ describe('The Resource state', () => {

expect(book.state.data.author.state.data.authorImage).not.toBe(authorImage);
expect(book.state.data.author.state.data.authorImage).not.toHaveProperty('setMetadata');
expect(book.state.data.author.state.data.authorImage).toEqual({ state: authorImage.state, type: authorImage.type });
expect(book.state.data.author.state.data.authorImage).toEqual({
state: authorImage.state,
type: authorImage.type,
});

expect(book.state.data.publication[0]).not.toBe(publication1);
expect(book.state.data.publication[0]).not.toHaveProperty('setMetadata');
expect(book.state.data.publication[0]).toEqual({ state: publication1.state, type: publication1.type });
expect(book.state.data.publication[0]).toEqual({
state: publication1.state,
type: publication1.type,
});
});

test('that resetting the state stack resets the state stack', () => {
const author = Resource.create(2, 'author', { name: 'A great author' });

expect(author.stateStack).toHaveLength(1);

author.resetStateStack();

expect(author.stateStack).toHaveLength(1);
expect(author.data.name).toEqual('A great author');

author.data = { ...author.data, name: 'Another author' };

expect(author.data.name).toEqual('Another author');
expect(author.stateStack).toHaveLength(2);

author.resetStateStack();

expect(author.data.name).toEqual('Another author');
expect(author.stateStack).toHaveLength(1);
});
});
8 changes: 8 additions & 0 deletions packages/core/src/Resource/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ function Resource(id, type, data = null, relationships = null, meta = null) {
return state;
},

resetStateStack() {
if (state.length === 1) {
return;
}

state.splice(0, state.length - 1);
},

/**
* @returns {Object}
*/
Expand Down

0 comments on commit 72d4a79

Please sign in to comment.