Skip to content

Commit

Permalink
Add tests for canonicalUrl filter
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Dec 23, 2023
1 parent 73c70f1 commit 2e09bfb
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions test/lib/filters/canonical-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const assert = require('assert/strict')
const { describe, it } = require('node:test')
const nunjucks = require('nunjucks')
const canonicalUrl = require('../../../lib/filters/canonical-url.js')

describe('canonicalUrl filter', () => {
const ctx = { options: { url: 'https://example.com' } }
const env = new nunjucks.Environment()
env.addFilter('canonicalUrl', canonicalUrl)

it('Returns given path if no site URL defined', () => {
const result = env.renderString('{{ "/path" | canonicalUrl }}', {
options: {}
})

assert.equal(result, '/path')
})

it('Returns external URL if doesn’t share hostname with site URL', () => {
const result = env.renderString(
'{{ "http://foo.bar" | canonicalUrl }}',
ctx
)

assert.equal(result, 'http://foo.bar')
})

it('Gets canonical site URL with resolved path', () => {
const result = env.renderString('{{ "/path" | canonicalUrl }}', ctx)

assert.equal(result, 'https://example.com/path')
})
})

0 comments on commit 2e09bfb

Please sign in to comment.