Skip to content

Commit

Permalink
proxy for api
Browse files Browse the repository at this point in the history
  • Loading branch information
amk221 committed Mar 1, 2023
1 parent 22eff72 commit f53fc2b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
56 changes: 32 additions & 24 deletions addon/components/expander/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,25 @@ import { action } from '@ember/object';
import { task } from 'ember-concurrency';
import { guidFor } from '@ember/object/internals';
import { waitForAnimation } from '@zestia/animation-utils';
const { seal, assign } = Object;
const { assign } = Object;

class ExpanderComponent extends Component {
@tracked maxHeight = null;
@tracked isExpanded = !!this.args.expanded;
@tracked renderContent = !!this.args.expanded;
@tracked isTransitioning = false;
@tracked maxHeight = null;
@tracked renderContent = !!this.args.expanded;

id = guidFor(this);

_api = {};
Content = null;
Button = null;
ExpanderContent = ExpanderContent;
Button;
Content;
contentElement = null;
ExpanderButton = ExpanderButton;
ExpanderContent = ExpanderContent;
id = guidFor(this);

registerComponents = (components) => {
assign(this, components);
};

get api() {
return seal(
assign(this._api, {
Content: this.renderContent ? this.Content : null,
Button: this.Button,
contentElement: this.contentElement,
toggle: this.toggle,
expand: this.expand,
collapse: this.collapse,
isExpanded: this.isExpanded,
isTransitioning: this.isTransitioning
})
);
}

get style() {
let style = '';

Expand Down Expand Up @@ -162,6 +146,30 @@ class ExpanderComponent extends Component {
transitionProperty: 'max-height'
});
}

api = new Proxy(this, {
get(target, key) {
if (
![
'Button',
'Content',
'contentElement',
'toggle',
'expand',
'collapse',
'isExpanded',
'isTransitioning'
].includes(key) ||
(key === 'Content' && !target.renderContent)
) {
return;
}

return target[key];
},

set() {}
});
}

export default ExpanderComponent;
30 changes: 11 additions & 19 deletions tests/integration/components/expander-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
waitUntil,
find
} from '@ember/test-helpers';
const { keys, isSealed } = Object;

module('expander', function (hooks) {
setupRenderingTest(hooks);
Expand Down Expand Up @@ -205,7 +204,7 @@ module('expander', function (hooks) {
});

test('api', async function (assert) {
assert.expect(9);
assert.expect(14);

this.handleReady = (expander) => (this.api = expander);

Expand All @@ -217,32 +216,25 @@ module('expander', function (hooks) {
</Expander>
`);

assert.deepEqual(
keys(this.api),
[
'Content',
'Button',
'contentElement',
'toggle',
'expand',
'collapse',
'isExpanded',
'isTransitioning'
],
'exposes the api when ready'
);

assert.true(isSealed(this.api));
assert.strictEqual(this.api.Content, undefined);
assert.strictEqual(typeof this.api.Button, 'object');
assert.deepEqual(this.api.contentElement, null);
assert.strictEqual(typeof this.api.toggle, 'function');
assert.strictEqual(typeof this.api.expand, 'function');
assert.strictEqual(typeof this.api.collapse, 'function');
assert.false(this.api.isExpanded);
assert.strictEqual(this.api.contentElement, undefined);
assert.false(this.api.isTransitioning);

assert.dom('.expander').doesNotIncludeText('Hello World');

this.api.expand();

await waitUntil(() => this.api.isTransitioning);
await settled();

assert.true(this.api.isExpanded);
assert.false(this.api.isTransitioning);
assert.strictEqual(typeof this.api.Content, 'object');
assert.deepEqual(this.api.contentElement, find('.expander__content'));
assert.dom('.expander').hasText('Hello World');
});
Expand Down

0 comments on commit f53fc2b

Please sign in to comment.