Skip to content

Commit

Permalink
feat(core): add support for RefElement and LinkElement (#3884)
Browse files Browse the repository at this point in the history
Refs #3882
  • Loading branch information
char0n authored Mar 1, 2024
1 parent 989f31d commit 342ee3f
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 9 deletions.
16 changes: 8 additions & 8 deletions packages/@types/minim.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,23 @@ declare module 'minim' {
}

export class LinkElement extends Element {
constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes);
constructor(content?: unknown, meta?: Meta, attributes?: Attributes);

get relation(): unknown;
get relation(): string;

set relation(relation: unknown);
set relation(relation: string);

get href(): unknown;
get href(): string;

set href(key: unknown);
set href(key: string);
}

export class RefElement extends Element {
constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes);
constructor(content?: unknown, meta?: Meta, attributes?: Attributes);

get path(): unknown;
get path(): string;

set path(path: unknown);
set path(path: string);
}

export class ArraySlice {
Expand Down
14 changes: 14 additions & 0 deletions packages/apidom-core/src/transformers/serializers/value/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
ObjectElement,
MemberElement,
NullElement,
RefElement,
LinkElement,
} from 'minim';

import { visit } from './visitor';
Expand Down Expand Up @@ -82,9 +84,21 @@ class Visitor {
public NullElement() {
return null;
}

public RefElement(element: RefElement): unknown {
return element.toValue();
}

public LinkElement(element: LinkElement): string {
if (isStringElement(element.href)) {
return element.href.toValue();
}
return '';
}
}

type ShortCutElementTypes = StringElement | NumberElement | BooleanElement | NullElement;

const serializer = <T extends Element | unknown>(element: T): any => {
if (!isElement(element)) return element;

Expand Down
42 changes: 42 additions & 0 deletions packages/apidom-core/test/transformers/serializers/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
NullElement,
ObjectElement,
ArrayElement,
RefElement,
LinkElement,
} from '../../../src';
import serializer from '../../../src/transformers/serializers/value';

Expand Down Expand Up @@ -134,6 +136,46 @@ describe('serializers', function () {
});
});

context('given RefElement', function () {
specify('should serialize to JavaScript value', function () {
const ref = new RefElement('id');
const serialized = serializer(ref);

assert.strictEqual(serialized, 'id');
});

context('and nested inside ObjectElement', function () {
specify('should serialize to JavaScript value', function () {
const object = new ObjectElement({
ref: new RefElement('id'),
});
const serialized = serializer(object);

assert.deepEqual(serialized, { ref: 'id' });
});
});
});

context('given LinkElement', function () {
specify('should serialize to JavaScript value', function () {
const ref = new LinkElement();
const serialized = serializer(ref);

assert.strictEqual(serialized, '');
});

context('and nested inside ObjectElement', function () {
specify('should serialize to JavaScript value', function () {
const object = new ObjectElement({
link: new LinkElement(undefined, undefined, { relation: 'foo', href: '/bar' }),
});
const serialized = serializer(object);

assert.deepEqual(serialized, { link: '/bar' });
});
});
});

context('given primitive value', function () {
specify('should acts as identify function', function () {
assert.strictEqual(serializer(1 as any), 1);
Expand Down
35 changes: 34 additions & 1 deletion packages/apidom-core/test/traversal/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { assert } from 'chai';
import sinon from 'sinon';

import {
ObjectElement,
ArrayElement,
StringElement,
visit,
MemberElement,
RefElement,
LinkElement,
visit,
toValue,
} from '../../src';

Expand Down Expand Up @@ -92,5 +95,35 @@ describe('traversal', function () {

assert.deepEqual(toValue(newArrayElement), [1]);
});

context('given RefElement', function () {
specify('should call RefElement visitor hook', function () {
const objectElement = new ObjectElement({
ref: new RefElement('id'),
});
const visitor = {
RefElement: sinon.spy(),
};

visit(objectElement, visitor);

assert.isTrue(visitor.RefElement.calledOnce);
});
});

context('given LinkElement', function () {
specify('should call LinkElement visitor hook', function () {
const objectElement = new ObjectElement({
ref: new LinkElement(),
});
const visitor = {
LinkElement: sinon.spy(),
};

visit(objectElement, visitor);

assert.isTrue(visitor.LinkElement.calledOnce);
});
});
});
});

0 comments on commit 342ee3f

Please sign in to comment.