Skip to content

Commit

Permalink
Merge pull request jdorn#730 from tohosaku/Fix_param_order
Browse files Browse the repository at this point in the history
Fix parameter order and Add test case
  • Loading branch information
schmunk42 authored Apr 27, 2020
2 parents c6db13e + 7b2ba67 commit 0d511c6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/schemaloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export class SchemaLoader {
this.refs[url] = 'loading'
waiting++

const fetchUrl = this._isLocalUrl(fileBase, url) ? fileBase + url : url
const fetchUrl = this._isLocalUrl(url, fileBase) ? fileBase + url : url

const r = new XMLHttpRequest()
r.overrideMimeType('application/json')
Expand Down
64 changes: 51 additions & 13 deletions tests/unit/schemaloader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,19 @@ describe('SchemaLoader', () => {
})

describe('when external ref exists', () => {
let response
let server

beforeAll(() => {
response = {
it('should set oprion { ajax: true }', done => {
const response = {
type: 'string',
minLength: 4
}
server = createFakeServer()
const server = createFakeServer()
server.autoRespond = true
window.XMLHttpRequest = server.xhr
server.respondWith([
200,
{ 'Content-Type': 'application/json' },
JSON.stringify(response)
])
})

afterAll(() => {
server.restore()
})

it('should set oprion { ajax: true }', done => {
fetchUrl =
document.location.origin + document.location.pathname.toString()
loader = new SchemaLoader({ ajax: true })
Expand All @@ -92,6 +82,54 @@ describe('SchemaLoader', () => {
const urls = Object.keys(loader.refs)
expect(urls.length).toEqual(1)
done()
server.restore()
},
fetchUrl,
fileBase
)
})
})

describe('when external relative $ref exists', () => {
it('can get refs recursively', done => {
const schema1 = {
type: 'object',
properties: {
fname: { $ref: '/schema/main.json' },
lname: { $ref: '/schema/main.json' }
}
}
const schema2 = {
$ref: 'registry/sub.json'
}
const schema3 = {
type: 'string',
minLength: 4
}
const server = createFakeServer()
server.autoRespond = true
window.XMLHttpRequest = server.xhr
server.respondWith('/schema/main.json', [
200,
{ 'Content-Type': 'application/json' },
JSON.stringify(schema2)
])
server.respondWith('/schema/registry/sub.json', [
200,
{ 'Content-Type': 'application/json' },
JSON.stringify(schema3)
])
fetchUrl =
document.location.origin + document.location.pathname.toString()
loader = new SchemaLoader({ ajax: true })
fileBase = loader._getFileBase(document.location.toString())
loader.load(
schema1,
schema => {
console.log(loader.refs_with_info)
expect(Object.keys(loader.refs).length).toBe(2)
done()
server.restore()
},
fetchUrl,
fileBase
Expand Down

0 comments on commit 0d511c6

Please sign in to comment.