Skip to content

Commit

Permalink
Merge pull request #382 from joshfrench/add-didrender-hook
Browse files Browse the repository at this point in the history
Add didrender hook
  • Loading branch information
bantic committed May 5, 2016
2 parents 3cb7831 + 1896a8b commit 8349f65
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/js/models/card-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@ export default class CardNode {
}
}

didRender() {
if (this._didRenderCallback) {
this._didRenderCallback();
}
}

get env() {
return {
name: this.card.name,
isInEditor: true,
onTeardown: (callback) => this._teardownCallback = callback,
didRender: (callback) => this._didRenderCallback = callback,
edit: () => this.edit(),
save: (payload, transition=true) => {
this.section.payload = payload;
Expand Down Expand Up @@ -90,5 +97,6 @@ export default class CardNode {
);
this.element.appendChild(rendered);
this._rendered = rendered;
this.didRender();
}
}
48 changes: 47 additions & 1 deletion tests/unit/editor/card-lifecycle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ function assertRenderArguments(assert, args, expected) {
assert.deepEqual(options, expected.options, 'correct options');

// basic env
let {name, isInEditor, onTeardown} = env;
let {name, isInEditor, onTeardown, didRender} = env;
assert.equal(name, expected.name, 'correct name');
assert.equal(isInEditor, expected.isInEditor, 'correct isInEditor');
assert.ok(!!onTeardown, 'has onTeardown');
assert.ok(!!didRender, 'has didRender');

// editor env hooks
let {save, cancel, edit, remove} = env;
Expand Down Expand Up @@ -586,3 +587,48 @@ test('onTeardown hook is called when editor is destroyed', (assert) => {

assert.ok(teardown, 'onTeardown hook called');
});

test('didRender hook is called when moving from display->edit and back', (assert) => {
const cardName = 'test-card';

let editHook;
let saveHook;
let currentMode;
let rendered;

const card = {
name: cardName,
type: 'dom',
render({env}) {
currentMode = 'display';
editHook = env.edit;
env.didRender(() => rendered = 'display');
return makeEl('display-card');
},
edit({env}) {
currentMode = 'edit';
saveHook = env.save;
env.didRender(() => rendered = 'edit');
return makeEl('edit-card');
}
};

const mobiledoc = Helpers.mobiledoc.build(({post, cardSection}) =>
post([cardSection(cardName)])
);
editor = new Editor({mobiledoc, cards: [card]});
editor.render(editorElement);

assert.equal(currentMode, 'display', 'precond - display mode');
assert.ok(rendered, 'didRender called on instantiation');

editHook();

assert.equal(currentMode, 'edit', 'edit mode');
assert.equal(rendered, 'edit', 'display didRender hook called');

saveHook();

assert.equal(currentMode, 'display', 'display mode');
assert.equal(rendered, 'display', 'edit didRender hook called');
});

0 comments on commit 8349f65

Please sign in to comment.