Update bookmark tests

This commit is contained in:
Ramon Wenger 2022-01-30 21:17:17 +01:00
parent b84c9c2d68
commit a988b15b76
7 changed files with 124 additions and 38 deletions

View File

@ -113,5 +113,6 @@ export default {
title: 'instrument-title', title: 'instrument-title',
slug: 'instrument-slug', slug: 'instrument-slug',
id: getInstrumentId(), id: getInstrumentId(),
bookmarks: null
}) })
}; };

View File

@ -36,6 +36,19 @@ describe('Bookmarks', () => {
] ]
} }
}, },
InstrumentQuery: {
instrument: {
contents: [
{
type: 'text_block',
value: {
text: 'Hallo Sam'
},
id: "df8212ee-3e82-49fa-977e-c4b60789163e"
}
]
}
},
UpdateLastModule: {}, UpdateLastModule: {},
UpdateContentBookmark: { UpdateContentBookmark: {
updateContentBookmark: { updateContentBookmark: {
@ -47,6 +60,11 @@ describe('Bookmarks', () => {
success: true success: true
} }
}, },
UpdateInstrumentBookmark: {
updateModuleBookmark: {
success: true
}
},
UpdateChapterBookmark: { UpdateChapterBookmark: {
updateChapterBookmark: { updateChapterBookmark: {
success: true success: true
@ -67,7 +85,31 @@ describe('Bookmarks', () => {
}); });
it('should bookmark instrument', () => { it('should bookmark instrument', () => {
cy.visit(); cy.visit('/instrument/an-instrument');
cy.getByDataCy('content-component').first().as('contentComponent');
cy.get('@contentComponent').within(() => {
cy.get('.bookmark-actions__bookmark').click();
cy.get('.bookmark-actions__add-note').click();
});
cy.get('[data-cy=bookmark-note]').within(() => {
cy.get('.skillbox-input').type('Hallo Velo');
});
cy.get('[data-cy=modal-save-button]').click();
cy.get('@contentComponent').within(() => {
cy.get('.bookmark-actions__edit-note').click();
});
cy.get('[data-cy=bookmark-note]').within(() => {
cy.get('.skillbox-input').clear().type('Hello Bike');
});
cy.get('[data-cy=modal-save-button]').click();
}); });
it('should bookmark module', () => { it('should bookmark module', () => {

View File

@ -52,17 +52,9 @@
import DELETE_PROJECT_ENTRY_MUTATION from '@/graphql/gql/mutations/deleteProjectEntry.gql'; import DELETE_PROJECT_ENTRY_MUTATION from '@/graphql/gql/mutations/deleteProjectEntry.gql';
import PROJECT_QUERY from '@/graphql/gql/queries/projectQuery.gql'; import PROJECT_QUERY from '@/graphql/gql/queries/projectQuery.gql';
import {dateFilter, dateTimeFilter} from '@/filters/date-filter'; import {dateFilter, dateTimeFilter} from '@/filters/date-filter';
import {removeAtIndex} from '@/graphql/immutable-operations';
const DocumentBlock = () => import(/* webpackChunkName: "content-components" */'@/components/content-blocks/DocumentBlock'); const DocumentBlock = () => import(/* webpackChunkName: "content-components" */'@/components/content-blocks/DocumentBlock');
const insertAtIndexImmutably = (arr, el, idx) => {
};
const removeAtIndexImmutably = (arr, idx) => {
return [
...arr.slice(0, idx),
...arr.slice(idx + 1) ,
];
};
export default { export default {
props: ['description', 'documentUrl', 'created', 'id', 'readOnly'], props: ['description', 'documentUrl', 'created', 'id', 'readOnly'],
@ -102,7 +94,7 @@
const {project} = store.readQuery({query, variables}); const {project} = store.readQuery({query, variables});
if (project) { if (project) {
const index = project.entries.findIndex(entry => entry.id === projectEntry.id); const index = project.entries.findIndex(entry => entry.id === projectEntry.id);
const entries = removeAtIndexImmutably(project.entries, index); const entries = removeAtIndex(project.entries, index);
const data = { const data = {
project: { project: {
...project, ...project,

View File

@ -0,0 +1,28 @@
// signature should be in order: arr, idx, el, for readability
export const pushToArray = (arr, el) => {
if (!arr) {
return [el];
}
return [...arr, el];
};
export const insertAtIndex = (arr, idx, el) => {
return [
...arr.slice(0, idx),
el,
...arr.slice(idx)
];
};
export const removeAtIndex = (arr, idx) => {
return [
...arr.slice(0, idx),
...arr.slice(idx + 1) ,
];
};
export const replaceAtIndex = (arr, idx, el) => {
console.log(idx);
return [
...arr.slice(0, idx),
el,
...arr.slice(idx+1)
];
};

View File

@ -5,6 +5,7 @@ import MODULE_QUERY from '@/graphql/gql/queries/modules/moduleDetailsQuery.gql';
import INSTRUMENT_FRAGMENT from '@/graphql/gql/fragments/instrumentParts.gql'; import INSTRUMENT_FRAGMENT from '@/graphql/gql/fragments/instrumentParts.gql';
import gql from 'graphql-tag'; import gql from 'graphql-tag';
import MODULE_FRAGMENT from '@/graphql/gql/fragments/moduleParts.gql'; import MODULE_FRAGMENT from '@/graphql/gql/fragments/moduleParts.gql';
import {replaceAtIndex} from '@/graphql/immutable-operations';
const compareUuid = note => element => element.uuid === note.content; const compareUuid = note => element => element.uuid === note.content;
@ -54,23 +55,34 @@ export const constructNoteMutation = (n) => {
}); });
} else { } else {
const fragment = INSTRUMENT_FRAGMENT; const fragment = INSTRUMENT_FRAGMENT;
const id = `InstrumentNode:${n.block}`; const id = store.identify({
const data = store.readFragment({ __typename: 'InstrumentNode',
slug: n.block
});
const instrument = store.readFragment({
fragment, fragment,
id id
}); });
const bookmarks = data.bookmarks; const prevBookmarks = instrument.bookmarks;
let bookmarks;
let index = bookmarks.findIndex(compareUuid(n)); let index = prevBookmarks.findIndex(compareUuid(n));
if (index > -1) { if (index > -1) {
let el = bookmarks[index]; let el = prevBookmarks[index];
el.note = note; bookmarks = replaceAtIndex(prevBookmarks, index, {
bookmarks.splice(index, 1, el); ...el,
note
});
} else {
bookmarks = [...prevBookmarks];
} }
data.bookmarks = bookmarks; const data = {
...instrument,
bookmarks
};
store.writeFragment({ store.writeFragment({
data, data,

View File

@ -2,6 +2,7 @@ import UPDATE_CONTENT_BOOKMARK from '@/graphql/gql/mutations/updateContentBookma
import UPDATE_INSTRUMENT_BOOKMARK from '@/graphql/gql/mutations/updateInstrumentBookmark.gql'; import UPDATE_INSTRUMENT_BOOKMARK from '@/graphql/gql/mutations/updateInstrumentBookmark.gql';
import CONTENT_BLOCK_QUERY from '@/graphql/gql/queries/contentBlockQuery.gql'; import CONTENT_BLOCK_QUERY from '@/graphql/gql/queries/contentBlockQuery.gql';
import INSTRUMENT_FRAGMENT from '@/graphql/gql/fragments/instrumentParts.gql'; import INSTRUMENT_FRAGMENT from '@/graphql/gql/fragments/instrumentParts.gql';
import {pushToArray, removeAtIndex} from '@/graphql/immutable-operations';
const compareUuid = uuid => element => element.uuid === uuid; const compareUuid = uuid => element => element.uuid === uuid;
@ -20,28 +21,37 @@ export const constructContentComponentBookmarkMutation = (uuid, bookmarked, pare
}, },
update: (store) => { update: (store) => {
const fragment = INSTRUMENT_FRAGMENT; const fragment = INSTRUMENT_FRAGMENT;
const id = `InstrumentNode:${root}`; const id = store.identify({
const data = store.readFragment({ __typename: 'InstrumentNode',
slug: root
});
const instrument = store.readFragment({
fragment, fragment,
id id
}); });
const bookmarks = data.bookmarks; let bookmarks;
if (bookmarked) { if (bookmarked) {
bookmarks.push({ const element = {
note: null, note: null,
uuid, uuid,
__typename: 'InstrumentBookmarkNode' __typename: 'InstrumentBookmarkNode'
}); };
bookmarks = pushToArray(instrument.bookmarks, element);
} else { } else {
let index = bookmarks.findIndex(compareUuid(uuid)); let index = instrument.bookmarks.findIndex(compareUuid(uuid));
if (index > -1) { if (index > -1) {
bookmarks.splice(index, 1); bookmarks = removeAtIndex(instrument.bookmarks, index);
} else {
bookmarks = [...instrument.bookmarks];
} }
} }
data.bookmarks = bookmarks; const data = {
...instrument,
bookmarks
};
store.writeFragment({ store.writeFragment({
data, data,
@ -79,23 +89,19 @@ export const constructContentComponentBookmarkMutation = (uuid, bookmarked, pare
let bookmarks; let bookmarks;
if (bookmarked) { if (bookmarked) {
bookmarks = [ let element = {
...contentBlock.bookmarks,
{
note: null, note: null,
uuid, uuid,
__typename: 'ContentBlockBookmarkNode' __typename: 'ContentBlockBookmarkNode'
} };
]; bookmarks = pushToArray(contentBlock.bookmarks, element);
} else { } else {
let index = bookmarks.findIndex(compareUuid(uuid)); let index = contentBlock.bookmarks.findIndex(compareUuid(uuid));
if (index > -1) { if (index > -1) {
bookmarks.splice(index, 1); bookmarks = removeAtIndex(contentBlock.bookmarks, index);
} else {
bookmarks = [...contentBlock.bookmarks];
} }
bookmarks = [
...contentBlock.bookmarks.slice(0, index),
...contentBlock.bookmarks.slice(index + 1)
];
} }
const data = { const data = {

View File

@ -0,0 +1,5 @@
describe('Cache operations', () => {
it('fails', () => {
expect(1).toEqual(2);
});
});