Skip to content

Commit

Permalink
display draft annotation while saving on backend
Browse files Browse the repository at this point in the history
resolves 2538
  • Loading branch information
esanzgar committed Sep 23, 2020
1 parent c0e5858 commit 6022d07
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/sidebar/components/annotation-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createElement } from 'preact';
import { useState } from 'preact/hooks';
import propTypes from 'prop-types';

import useStore from '../store/use-store';
import { isHidden } from '../util/annotation-metadata';
import { withServices } from '../util/service-context';
import { applyTheme } from '../util/theme';
Expand Down Expand Up @@ -36,9 +37,13 @@ function AnnotationBody({ annotation, settings }) {
// collapsing/expanding is relevant?
const [isCollapsible, setIsCollapsible] = useState(false);

const draft = useStore(store => store.getDraft(annotation));

const toggleText = isCollapsed ? 'More' : 'Less';
const tags = annotation.tags;
const text = annotation.text;

// If there is a draft use the tag and text from it.
const tags = draft?.tags ?? annotation.tags;
const text = draft?.text ?? annotation.text;
const showExcerpt = text.length > 0;
const showTagList = tags.length > 0;

Expand Down
43 changes: 43 additions & 0 deletions src/sidebar/components/test/annotation-body-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ describe('AnnotationBody', () => {
let fakeApplyTheme;
let fakeSettings;

// Inject dependency mocks
let fakeStore;

const setEditingMode = (isEditing = true) => {
// The presence of a draft will make `isEditing` `true`
if (isEditing) {
fakeStore.getDraft.returns({
...fixtures.defaultDraft(),
text: 'this is a draft',
tags: ['1', '2'],
});
} else {
fakeStore.getDraft.returns(null);
}
};

function createBody(props = {}) {
return mount(
<AnnotationBody
Expand All @@ -32,16 +48,43 @@ describe('AnnotationBody', () => {
fakeApplyTheme = sinon.stub();
fakeSettings = {};

fakeStore = {
getDraft: sinon.stub().returns(null),
};

$imports.$mock(mockImportedComponents());
$imports.$mock({
'../util/theme': { applyTheme: fakeApplyTheme },
'../store/use-store': callback => callback(fakeStore),
});
});

afterEach(() => {
$imports.$restore();
});

it('renders the tags and text from the annotation', () => {
const wrapper = createBody();
wrapper.update();

const markdownView = wrapper.find('MarkdownView');
const tagList = wrapper.find('TagList');
assert.strictEqual(markdownView.props().markdown, 'some text here');
assert.deepStrictEqual(tagList.props().tags, ['eenie', 'minie']);
});

it('renders the tags and text from the draft', () => {
setEditingMode(true);

const wrapper = createBody();
wrapper.update();

const markdownView = wrapper.find('MarkdownView');
const tagList = wrapper.find('TagList');
assert.strictEqual(markdownView.props().markdown, 'this is a draft');
assert.deepStrictEqual(tagList.props().tags, ['1', '2']);
});

it('does not render controls to expand/collapse the excerpt if it is not collapsible', () => {
const wrapper = createBody();

Expand Down

0 comments on commit 6022d07

Please sign in to comment.