Skip to content

Commit

Permalink
fix: fix bug of type
Browse files Browse the repository at this point in the history
  • Loading branch information
ymmooot committed May 31, 2019
1 parent 692ca43 commit 9e7c807
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 27 deletions.
4 changes: 2 additions & 2 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"dependencies": {
"cross-env": "^5.2.0",
"nuxt": "^2.0.0",
"nuxt-jsonld": "file:../../lib/",
"ts-node": "^8.2.0",
"vue-property-decorator": "^8.1.1",
"nuxt-jsonld": "file:../../lib"
"vue-property-decorator": "^8.1.1"
},
"devDependencies": {
"@nuxt/typescript": "^2.7.1",
Expand Down
5 changes: 4 additions & 1 deletion src/createMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default (options: Options = {}): JsonldMixin => {
...options,
};

let counter = 0;

return {
head(this: Vue) {
if (!this.$options || typeof this.$options.jsonld !== 'function') {
Expand All @@ -34,7 +36,8 @@ export default (options: Options = {}): JsonldMixin => {
const stringifiedJson = JSON.stringify(this.$options.jsonld.call(this), null, mergedOptions.space);
const innerHTML = mergedOptions.space === 0 ? stringifiedJson : `\n${stringifiedJson}\n`;

const hid = `nuxt-jsonld-${this._uid}`;
const hid = `nuxt-jsonld-${counter}`;
counter += 1;
return {
script: [
{
Expand Down
13 changes: 0 additions & 13 deletions src/declaration.d.ts

This file was deleted.

8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import './declaration.d';
import Vue, { ComponentOptions } from 'vue';
import createJsonldMixin from './createMixin';
import decorator from './decorator';

declare module 'vue/types/options' {
interface ComponentOptions<V extends Vue> {
jsonld?: () => object;
}
}

const getValue = (val, context) => {
if (typeof val === 'object') {
return val;
Expand Down
63 changes: 53 additions & 10 deletions test/createMixin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@ describe('with jsonld', () => {
test('head method returns jsonld metaInfo', () => {
const mock = mockInstanceFactory();

const mockHid = `nuxt-jsonld-${mock._uid}`;

expect(mock.$options.head.call(mock)).toEqual({
__dangerouslyDisableSanitizersByTagID: {
[mockHid]: 'innerHTML',
'nuxt-jsonld-0': 'innerHTML',
},
script: [
{
hid: mockHid,
hid: 'nuxt-jsonld-0',
innerHTML: `
{
"@context": "http://schema.org",
Expand Down Expand Up @@ -94,15 +92,14 @@ describe('with jsonld', () => {
describe('customizing indentation', () => {
test('using tab', () => {
const mock = mockInstanceFactory({ space: '\t' });
const mockHid = `nuxt-jsonld-${mock._uid}`;

expect(mock.$options.head.call(mock)).toEqual({
__dangerouslyDisableSanitizersByTagID: {
[mockHid]: 'innerHTML',
'nuxt-jsonld-0': 'innerHTML',
},
script: [
{
hid: mockHid,
hid: 'nuxt-jsonld-0',
innerHTML: `
{
"@context": "http://schema.org",
Expand Down Expand Up @@ -132,15 +129,14 @@ describe('with jsonld', () => {
});
test('no space', () => {
const mock = mockInstanceFactory({ space: 0 });
const mockHid = `nuxt-jsonld-${mock._uid}`;

expect(mock.$options.head.call(mock)).toEqual({
__dangerouslyDisableSanitizersByTagID: {
[mockHid]: 'innerHTML',
'nuxt-jsonld-0': 'innerHTML',
},
script: [
{
hid: mockHid,
hid: 'nuxt-jsonld-0',
innerHTML: `{"@context":"http://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://example.com/"}},{"@type":"ListItem","position":2,"item":{"@id":"https://example.com/foo/"}}]}`,
type: 'application/ld+json',
},
Expand All @@ -149,3 +145,50 @@ describe('with jsonld', () => {
});
});
});

describe('hid', () => {
test('increase hid number sufix', () => {
const mixin = createJsonldMixin({ space: 0 });
const mock1 = new Vue({
mixins: [mixin],
jsonld() {
return {};
},
});
const mock2 = new Vue({
mixins: [mixin],
jsonld() {
return {};
},
});
const mock3 = new Vue({
mixins: [mixin],
jsonld() {
return {};
},
});

const actual = [mock1, mock2, mock3].map(mock => mock.$options.head.call(mock));

expect(actual).toEqual([
{
__dangerouslyDisableSanitizersByTagID: {
'nuxt-jsonld-0': 'innerHTML',
},
script: [{ hid: 'nuxt-jsonld-0', innerHTML: '{}', type: 'application/ld+json' }],
},
{
__dangerouslyDisableSanitizersByTagID: {
'nuxt-jsonld-1': 'innerHTML',
},
script: [{ hid: 'nuxt-jsonld-1', innerHTML: '{}', type: 'application/ld+json' }],
},
{
__dangerouslyDisableSanitizersByTagID: {
'nuxt-jsonld-2': 'innerHTML',
},
script: [{ hid: 'nuxt-jsonld-2', innerHTML: '{}', type: 'application/ld+json' }],
},
]);
});
});

0 comments on commit 9e7c807

Please sign in to comment.