-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
entity-renderer: define basic tests using mocha
- Loading branch information
1 parent
0ef50be
commit 313a9fe
Showing
7 changed files
with
189 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,5 @@ middlewares: | |
```sh | ||
npm run example-instance | ||
``` | ||
|
||
And go to http://localhost:3000/ to see the result. |
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,23 @@ | ||
import { join } from 'path' | ||
import trifid from 'trifid-core' | ||
import entityRendererTrifidPlugin from '../index.js' | ||
|
||
const port = 3000 | ||
|
||
export const createTrifidInstance = async (configFilePath, logLevel = 'debug') => { | ||
const configFile = join(process.cwd(), configFilePath) | ||
return await trifid({ | ||
extends: [configFile], | ||
server: { | ||
logLevel, | ||
listener: { | ||
port, | ||
host: '0.0.0.0', | ||
}, | ||
}, | ||
}, { | ||
entityRenderer: { | ||
module: entityRendererTrifidPlugin, | ||
}, | ||
}) | ||
} |
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,30 +1,7 @@ | ||
#!/usr/bin/env node | ||
|
||
/* eslint-disable no-console */ | ||
import { createTrifidInstance } from './instance.js' | ||
|
||
import { join } from 'path' | ||
import express from 'express' | ||
const trifidInstance = await createTrifidInstance('examples/config/trifid.yaml', 'debug') | ||
|
||
import trifid from 'trifid-core' | ||
|
||
async function createTrifidInstance (filePath) { | ||
const configFile = join(process.cwd(), filePath) | ||
const config = { | ||
extends: [configFile], | ||
server: { | ||
listener: {}, | ||
}, | ||
} | ||
return await trifid(config) | ||
} | ||
|
||
const test = await createTrifidInstance('examples/config/trifid.yaml') | ||
|
||
const app = express() // The main app | ||
const PORT = 3000 | ||
|
||
app.use('/', test.server) | ||
app.listen(PORT, function (err) { | ||
if (err) console.log(err) | ||
console.log('Server listening on PORT', PORT) | ||
}) | ||
await trifidInstance.start() |
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,51 @@ | ||
// @ts-check | ||
|
||
/* eslint-disable no-useless-catch */ | ||
|
||
import { strictEqual } from 'assert' | ||
import { describe, it } from 'mocha' | ||
|
||
import { createTrifidInstance } from '../examples/instance.js' | ||
import { getListenerURL } from './support/utils.js' | ||
|
||
const trifidConfigUrl = './examples/config/trifid.yaml' | ||
|
||
describe('@zazuko/trifid-plugin-ckan', () => { | ||
describe('basic tests', () => { | ||
it('should create a middleware with factory and default options', async () => { | ||
const trifidInstance = await createTrifidInstance(trifidConfigUrl, 'warn') | ||
const trifidListener = await trifidInstance.start() | ||
trifidListener.close() | ||
}) | ||
|
||
it('should be able to load a rendered entity', async () => { | ||
const trifidInstance = await createTrifidInstance(trifidConfigUrl, 'warn') | ||
const trifidListener = await trifidInstance.start() | ||
|
||
try { | ||
const entityUrl = `${getListenerURL(trifidListener)}/person/amy-farrah-fowler` | ||
const res = await fetch(entityUrl) | ||
strictEqual(res.status, 200) | ||
} catch (e) { | ||
throw e | ||
} finally { | ||
trifidListener.close() | ||
} | ||
}) | ||
|
||
it('should not render non-existant entity', async () => { | ||
const trifidInstance = await createTrifidInstance(trifidConfigUrl, 'warn') | ||
const trifidListener = await trifidInstance.start() | ||
|
||
try { | ||
const entityUrl = `${getListenerURL(trifidListener)}/person/someone-that-does-not-exist` | ||
const res = await fetch(entityUrl) | ||
strictEqual(res.status, 404) | ||
} catch (e) { | ||
throw e | ||
} finally { | ||
trifidListener.close() | ||
} | ||
}) | ||
}) | ||
}) |
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,20 @@ | ||
// @ts-check | ||
|
||
/** | ||
* Get the URL of a listener. | ||
* | ||
* @param {import('http').Server} listener HTTP listener | ||
* @returns {string} | ||
*/ | ||
export const getListenerURL = (listener) => { | ||
const address = listener.address() | ||
if (!address) { | ||
throw new Error('The listener is not listening') | ||
} | ||
if (typeof address === 'string') { | ||
return address | ||
} | ||
|
||
const { address: hostname, port } = address | ||
return `http://${hostname}:${port}` | ||
} |