Skip to content

Commit

Permalink
feat(define): Add support for a static "is" property.
Browse files Browse the repository at this point in the history
implements #1004
  • Loading branch information
treshugart committed Jan 7, 2017
1 parent 7534ebd commit 0f0611b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/api/define.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function (...args) {
throw new Error('When passing only one argument to define(), it must be a custom element constructor.');
} else {
Ctor = name;
name = uniqueId();
name = Ctor.is || uniqueId();
}
}

Expand All @@ -41,7 +41,7 @@ export default function (...args) {
// performance but still falling back to a robust method.
Ctor[$name] = name;

// Sipmle define. Not supporting customised built-ins yet.
// Simple define. Not supporting customised built-ins yet.
customElements.define(name, Ctor);

// The spec doesn't return but this allows for a simpler, more concise API.
Expand Down
34 changes: 27 additions & 7 deletions test/unit/api/define.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,36 @@ describe('api/define', () => {
}).to.not.throw();
});

// DEPRECATED
it('should take an object and extend Component', () => {
expect(define('x-test', class extends Component {}).prototype).to.be.an.instanceof(Component);
it('static "is" should be used as "name" argument', () => {
const name = 'x-test-api-define-is';
const Elem = define(class extends Component {
static is = name
});
const elem = new Elem();
expect(elem.localName).to.equal(name);
});

it('should be able to take an ES5 extension of Component', () => {
expect(define('x-test', class extends Component {}).prototype).to.be.an.instanceof(Component);
it('static "is" should not override "name" argument', () => {
const name1 = 'x-test-api-define-is-override';
const name2 = 'x-test-api-define-is-not-override';
const Elem = define(name2, class extends Component {
static is = name1
});
const elem = new Elem();
expect(elem.localName).to.equal(name2);
});

it('should take a constructor that extends Component', () => {
expect(define('x-test', class extends Component {}).prototype).to.be.an.instanceof(Component);
describe('deprecated', () => {
it('should take an object and extend Component', () => {
expect(define('x-test', class extends Component {}).prototype).to.be.an.instanceof(Component);
});

it('should be able to take an ES5 extension of Component', () => {
expect(define('x-test', class extends Component {}).prototype).to.be.an.instanceof(Component);
});

it('should take a constructor that extends Component', () => {
expect(define('x-test', class extends Component {}).prototype).to.be.an.instanceof(Component);
});
});
});

0 comments on commit 0f0611b

Please sign in to comment.