-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from empress/test-meta
Add tests for metadata and fix anything that is obviously wrong
- Loading branch information
Showing
13 changed files
with
225 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import HeadData from 'ember-meta/services/head-data'; | ||
import { getOwner } from '@ember/application'; | ||
import { computed } from '@ember/object'; | ||
import config from 'ember-get-config'; | ||
|
||
import { getExcerpt, stripHTML } from '../helpers/excerpt'; | ||
|
||
const { blog } = config; | ||
|
||
export default class HeadDataService extends HeadData { | ||
get config() { | ||
return config['blog']; | ||
} | ||
|
||
@computed('config.title') | ||
get siteName() { | ||
return this.config.title; | ||
} | ||
|
||
@computed('routeName') | ||
get currentRouteMeta() { | ||
let currentController = getOwner(this).lookup(`controller:${this.routeName}`) | ||
|
||
return currentController.model.post ?? currentController.model; | ||
} | ||
|
||
@computed('currentRouteMeta.name', 'routeName') | ||
get title() { | ||
if(this.routeName === 'tag') { | ||
return `Tag: ${this.currentRouteMeta.name}`; | ||
} | ||
|
||
if(this.routeName === 'author') { | ||
return `Author: ${this.currentRouteMeta.name}`; | ||
} | ||
|
||
return super.title; | ||
} | ||
|
||
@computed('currentRouteMeta') | ||
get description() { | ||
let currentModel = this.currentRouteMeta; | ||
|
||
if(currentModel && currentModel.html) { | ||
const excerpt = getExcerpt(currentModel.html, { | ||
words: 33 | ||
}) | ||
|
||
return `${excerpt}${excerpt.length !== stripHTML(currentModel.html).length ? '...' : ''}`; | ||
} | ||
|
||
return blog.description; | ||
} | ||
|
||
@computed('currentRouteMeta.id') | ||
get slug() { | ||
return this.currentRouteMeta?.id; | ||
} | ||
|
||
@computed('currentRouteMeta.tags') | ||
get categories() { | ||
return this.currentRouteMeta?.tags?.mapBy('name'); | ||
} | ||
|
||
@computed('currentRouteMeta.image') | ||
get imgSrc() { | ||
let url = blog.host ? `${blog.host}` : ''; | ||
|
||
url += this.currentRouteMeta.image || blog.rssLogo || blog.logo; | ||
|
||
return url; | ||
} | ||
|
||
@computed('router.currentURL') | ||
get url() { | ||
// url is only ever valid if you have a host | ||
if(!blog.host) { | ||
return null; | ||
} | ||
|
||
// we remove any trailing / from the host and add it back in to make sure | ||
// that we always have a consistent URL | ||
const normalisedHost = blog.host.replace(/\/$/, ''); | ||
const normalisedUrl = this.router.currentURL.replace(/\/$/, ''); | ||
|
||
return `${normalisedHost}${normalisedUrl}/`; | ||
} | ||
|
||
@computed('routeName') | ||
get type() { | ||
if(this.routeName === 'post') { | ||
return 'article'; | ||
} | ||
|
||
return 'website'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1 @@ | ||
/* eslint-disable ember/require-computed-macros, ember/require-computed-property-dependencies, ember/no-get, ember/require-return-from-computed */ | ||
import HeadData from 'ember-meta/services/head-data'; | ||
import { computed, get } from '@ember/object'; | ||
import { getOwner } from '@ember/application'; | ||
import config from 'ember-get-config'; | ||
|
||
import { getExcerpt } from '../helpers/excerpt'; | ||
|
||
const { blog } = config; | ||
|
||
export default HeadData.extend({ | ||
author: computed('routeName', function() { | ||
return this.get('currentRouteModel.author.name'); | ||
}), | ||
|
||
currentRouteModel: computed('routeName', function() { | ||
return getOwner(this).lookup(`route:${this.get('routeName')}`).get('currentModel.post') || {}; | ||
}), | ||
|
||
description: computed('routeName', function() { | ||
let currentModel = this.get('currentRouteModel'); | ||
|
||
if(currentModel && get(currentModel, 'html')) { | ||
const excerpt = getExcerpt(get(currentModel, 'html'), { | ||
words: 33 | ||
}) | ||
return `${excerpt}...`; | ||
} | ||
|
||
return blog.description; | ||
}), | ||
|
||
slug: computed('routeName', function() { | ||
return this.get('currentRouteModel.id'); | ||
}), | ||
|
||
categories: computed('routeName', function() { | ||
let tags = this.get('currentRouteModel.tags') | ||
|
||
if(tags) { | ||
return tags.mapBy('name'); | ||
} | ||
}), | ||
|
||
imgSrc: computed('routeName', function() { | ||
let url = blog.host ? `${blog.host}` : ''; | ||
|
||
url += this.currentRouteModel.image || blog.rssLogo || blog.logo; | ||
|
||
return url; | ||
}), | ||
|
||
url: computed('routeName', function() { | ||
if(!blog.host || !this.slug) { return; } | ||
|
||
return `${blog.host}/${this.slug}/`; | ||
}) | ||
}); | ||
export { default } from 'empress-blog/services/head-data'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { module, test } from 'qunit'; | ||
import { visit } from '@ember/test-helpers'; | ||
import { setupApplicationTest } from 'ember-qunit'; | ||
|
||
module('Acceptance | meta test', function(hooks) { | ||
setupApplicationTest(hooks); | ||
|
||
test('index meta', async function(assert) { | ||
await visit('/'); | ||
|
||
assert.dom('head meta[property="og:title"]', document) | ||
.hasAttribute('content', 'empress-blog: The simple JAM stack blog'); | ||
assert.dom('head meta[name="description"]', document) | ||
.hasAttribute('content', 'Thoughts, stories and ideas.'); | ||
assert.dom('head link[rel="canonical"]', document) | ||
.hasAttribute('href', 'https://empress-blog.netlify.com/'); | ||
assert.dom('head meta[property="og:type"]', document) | ||
.hasAttribute('content', 'website'); | ||
assert.dom('head meta[property="og:image"]', document) | ||
.hasAttribute('content', 'https://empress-blog.netlify.com/images/logo.png'); | ||
assert.dom('head meta[property="og:site_name"]', document) | ||
.hasAttribute('content', 'empress-blog: The simple JAM stack blog'); | ||
}); | ||
|
||
test('content meta', async function(assert) { | ||
await visit('/welcome'); | ||
|
||
assert.dom('head meta[property="og:title"]', document) | ||
.hasAttribute('content', 'Welcome to empress-blog'); | ||
assert.dom('head meta[name="description"]', document) | ||
.hasAttribute('content', `Hey! Welcome to empress-blog, it's great to have you :) We know that first impressions are important, so we've populated your new site with some initial Getting Started posts that will...`); | ||
assert.dom('head meta[name="twitter:label1"]', document) | ||
.hasAttribute('content', 'Written by'); | ||
assert.dom('head meta[name="twitter:data1"]', document) | ||
.hasAttribute('content', 'Ghost'); | ||
assert.dom('head meta[name="twitter:label2"]', document) | ||
.hasAttribute('content', 'Filed under'); | ||
assert.dom('head meta[name="twitter:data2"]', document) | ||
.hasAttribute('content', 'Getting Started, Recent posts'); | ||
assert.dom('head link[rel="canonical"]', document) | ||
.hasAttribute('href', 'https://empress-blog.netlify.com/welcome/'); | ||
// I'm not testing the value of conent here to avoid timezone variations | ||
assert.dom('head meta[property="article:published_time"]', document) | ||
.hasAttribute('content'); | ||
assert.dom('head meta[property="article:tag"]', document) | ||
.hasAttribute('content', 'Getting Started'); | ||
assert.dom('head meta[property="og:type"]', document) | ||
.hasAttribute('content', 'article'); | ||
assert.dom('head meta[property="og:image"]', document) | ||
.hasAttribute('content', 'https://empress-blog.netlify.com/images/welcome.jpg'); | ||
}) | ||
|
||
test('page meta', async function(assert) { | ||
await visit('/page/chris-manson'); | ||
|
||
assert.dom('head meta[property="og:title"]', document) | ||
.hasAttribute('content', 'Built with ❤️ by Chris Manson'); | ||
assert.dom('head meta[name="description"]', document) | ||
.hasAttribute('content', `Chris Manson is a Ember enthusiast and a member of the Ember Core Learning Team. empress-blog was an experimental project that came out of the work on converting the Ember Guides to...`); | ||
assert.dom('head meta[name="twitter:label1"]', document) | ||
.hasAttribute('content', 'Written by'); | ||
assert.dom('head meta[name="twitter:data1"]', document) | ||
.hasAttribute('content', 'Chris Manson'); | ||
assert.dom('head link[rel="canonical"]', document) | ||
.hasAttribute('href', 'https://empress-blog.netlify.com/page/chris-manson/'); | ||
assert.dom('head meta[property="og:type"]', document) | ||
.hasAttribute('content', 'website'); | ||
assert.dom('head meta[property="og:image"]', document) | ||
.hasAttribute('content', 'https://empress-blog.netlify.com/images/built-by.jpg'); | ||
}); | ||
|
||
test('author meta', async function(assert) { | ||
await visit('/author/ghost'); | ||
|
||
assert.dom('head meta[property="og:title"]', document) | ||
.hasAttribute('content', 'Author: Ghost'); | ||
assert.dom('head meta[name="description"]', document) | ||
.hasAttribute('content', 'You can delete this user to remove all the welcome posts'); | ||
assert.dom('head link[rel="canonical"]', document) | ||
.hasAttribute('href', 'https://empress-blog.netlify.com/author/ghost/'); | ||
assert.dom('head meta[property="og:type"]', document) | ||
.hasAttribute('content', 'website'); | ||
assert.dom('head meta[property="og:image"]', document) | ||
.hasAttribute('content', 'https://empress-blog.netlify.com/images/ghost-icon.png'); | ||
}); | ||
|
||
test('tag meta', async function(assert) { | ||
await visit('/tag/getting-started'); | ||
|
||
assert.dom('head meta[property="og:title"]', document) | ||
.hasAttribute('content', 'Tag: Getting Started'); | ||
assert.dom('head meta[name="description"]', document) | ||
.hasAttribute('content', 'A description for the getting-started tag. If you delete this line it will say A collection of X posts where the description should go.'); | ||
assert.dom('head link[rel="canonical"]', document) | ||
.hasAttribute('href', 'https://empress-blog.netlify.com/tag/getting-started/'); | ||
assert.dom('head meta[property="og:type"]', document) | ||
.hasAttribute('content', 'website'); | ||
assert.dom('head meta[property="og:image"]', document) | ||
.hasAttribute('content', 'https://empress-blog.netlify.com/images/logo.png'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.