-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9e72af
commit 165ef88
Showing
3 changed files
with
138 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import fs from 'fs'; | ||
import http from 'http'; | ||
import path from 'path'; | ||
import url from 'url'; | ||
import puppeteer from 'puppeteer'; | ||
|
||
describe('Test browser Parser in the node env', function() { | ||
let server: http.Server; | ||
let browser: puppeteer.Browser; | ||
let page: puppeteer.Page; | ||
|
||
beforeAll(async function() { | ||
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); | ||
const htmlPath = path.resolve(__dirname, 'sample-page.html'); | ||
const parserScript = path.resolve(__dirname, '../../browser_new/index.js'); | ||
|
||
console.info('start server'); | ||
server = http.createServer((req, res) => { | ||
res.writeHead(200, { 'content-type': 'text/html' }); | ||
if (req.url === '/') { | ||
return fs.createReadStream(htmlPath).pipe(res); | ||
} else if (req.url === '/parser.js') { | ||
return fs.createReadStream(parserScript).pipe(res); | ||
} | ||
}); | ||
server.listen(8080); | ||
|
||
//use this in case you want to troubleshoot in a real chrome window => browser = await puppeteer.launch({headless: false}); | ||
console.info('starting browser'); | ||
browser = await puppeteer.launch(); | ||
|
||
console.info('opening new page'); | ||
page = await browser.newPage(); | ||
|
||
page.on('console', msg => { | ||
msg.args().forEach((arg, index) => { | ||
console.error(`Browser console content ${index}: ${JSON.stringify(arg.remoteObject().value, null, 2)}`); | ||
}); | ||
}); | ||
|
||
console.info('navigating to localhost'); | ||
await page.goto('http://localhost:8080', { waitUntil: 'networkidle0' }); | ||
}); | ||
|
||
afterAll(async function() { | ||
await browser.close(); | ||
server.close(); | ||
}); | ||
|
||
it('should parse spec in the browser', async function() { | ||
console.info('getting content element'); | ||
const contentDiv = await page.$('#content'); | ||
const content = await page.evaluate(element => element && element.textContent, contentDiv); | ||
expect(content).toEqual('2.0.0'); | ||
|
||
console.info('getting number of warnings'); | ||
const diagnosticsDiv = await page.$('#diagnostics'); | ||
const diagnostics = await page.evaluate(element => element && element.textContent, diagnosticsDiv); | ||
expect(Number(diagnostics)).toBeGreaterThanOrEqual(0); | ||
}, 5000); | ||
}); |
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,29 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Sample page</title> | ||
<meta charset="utf-8" /> | ||
</head> | ||
|
||
<body> | ||
<div id="content"></div> | ||
<div id="diagnostics"></div> | ||
</body> | ||
|
||
<script src="./parser.js"></script> | ||
<script defer> | ||
async function parse() { | ||
try { | ||
const parser = new window.AsyncAPIParser.Parser(); | ||
const spec = '{ "asyncapi": "2.0.0", "info": { "title": "My API", "version": "1.0.0" }, "channels": { "/test/tester": { "subscribe": { "operationId": "subscribeOperation", "message": { } } } } }'; | ||
const { document: parsedDocument, diagnostics } = await parser.parse(spec); | ||
|
||
document.getElementById('content').innerHTML = parsedDocument.version(); | ||
document.getElementById('diagnostics').innerHTML = String(diagnostics.length); | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
parse(); | ||
</script> | ||
</html> |
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,48 @@ | ||
/* eslint-disable */ | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
entry: './src/index.ts', | ||
target: 'web', | ||
mode: 'production', | ||
|
||
output: { | ||
path: path.resolve(__dirname, 'browser_new'), | ||
filename: 'index.js', | ||
globalObject: '(typeof self !== \'undefined\' ? self : this)', | ||
library: { | ||
name: 'AsyncAPIParser', | ||
type: 'umd' | ||
}, | ||
}, | ||
|
||
module: { | ||
rules: [ | ||
{ | ||
test: /\.tsx?$/, | ||
loader: 'ts-loader', | ||
exclude: /node_modules/, | ||
options: { | ||
configFile: 'tsconfig.json', | ||
transpileOnly: true, | ||
}, | ||
}, | ||
], | ||
}, | ||
resolve: { | ||
extensions: ['.ts', '.tsx', '.js'], | ||
fallback: { | ||
fs: false, | ||
path: false, | ||
util: false, | ||
buffer: require.resolve('buffer/'), | ||
} | ||
}, | ||
|
||
plugins: [ | ||
/** | ||
* Uncomment plugin when you wanna see dependency map of bundled package | ||
*/ | ||
// (require('webpack-bundle-analyzer').BundleAnalyzerPlugin()), | ||
], | ||
}; |