-
-
Notifications
You must be signed in to change notification settings - Fork 126
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
4b14d93
commit 9f4219c
Showing
4 changed files
with
219 additions
and
56 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 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { Sidebar } from '../Sidebar'; | ||
import { ConfigContext, SpecificationContext } from '../../../contexts'; | ||
import asyncapi from '../../../__tests__/docs/v3/streetlights-kafka.json'; | ||
import { Parser } from '../../../helpers'; | ||
import { AsyncAPIDocumentInterface } from '@asyncapi/parser'; | ||
describe('Sidebar component', () => { | ||
let parsed: AsyncAPIDocumentInterface; | ||
beforeAll(async () => { | ||
const parsedDoc = await Parser.parse(asyncapi, {}); | ||
expect(parsedDoc.error).toBeUndefined(); | ||
expect(parsedDoc.asyncapi).toBeDefined(); | ||
parsed = parsedDoc.asyncapi!; | ||
}); | ||
test('should render sidebar with showOperations: byDefault', async () => { | ||
render( | ||
<ConfigContext.Provider | ||
value={{ sidebar: { showOperations: 'byDefault' } }} | ||
> | ||
<SpecificationContext.Provider value={parsed}> | ||
<Sidebar /> | ||
</SpecificationContext.Provider> | ||
</ConfigContext.Provider>, | ||
); | ||
}); | ||
test('should render sidebar with showOperations: byOperationsTags', async () => { | ||
render( | ||
<ConfigContext.Provider | ||
value={{ sidebar: { showOperations: 'byOperationsTags' } }} | ||
> | ||
<SpecificationContext.Provider value={parsed}> | ||
<Sidebar /> | ||
</SpecificationContext.Provider> | ||
</ConfigContext.Provider>, | ||
); | ||
}); | ||
test('should render sidebar with showOperations: bySpecTags', async () => { | ||
render( | ||
<ConfigContext.Provider | ||
value={{ sidebar: { showOperations: 'bySpecTags' } }} | ||
> | ||
<SpecificationContext.Provider value={parsed}> | ||
<Sidebar /> | ||
</SpecificationContext.Provider> | ||
</ConfigContext.Provider>, | ||
); | ||
}); | ||
test('should render sidebar with showServers: byDefault', async () => { | ||
render( | ||
<ConfigContext.Provider value={{ sidebar: { showServers: 'byDefault' } }}> | ||
<SpecificationContext.Provider value={parsed}> | ||
<Sidebar /> | ||
</SpecificationContext.Provider> | ||
</ConfigContext.Provider>, | ||
); | ||
}); | ||
test('should render sidebar with showServers: byServersTags', async () => { | ||
render( | ||
<ConfigContext.Provider | ||
value={{ sidebar: { showServers: 'byServersTags' } }} | ||
> | ||
<SpecificationContext.Provider value={parsed}> | ||
<Sidebar /> | ||
</SpecificationContext.Provider> | ||
</ConfigContext.Provider>, | ||
); | ||
}); | ||
test('should render sidebar with showServers: bySpecTags', async () => { | ||
render( | ||
<ConfigContext.Provider | ||
value={{ sidebar: { showServers: 'bySpecTags' } }} | ||
> | ||
<SpecificationContext.Provider value={parsed}> | ||
<Sidebar /> | ||
</SpecificationContext.Provider> | ||
</ConfigContext.Provider>, | ||
); | ||
}); | ||
test('should render with showOperations: byDefault, showServers: byDefault', async () => { | ||
render( | ||
<ConfigContext.Provider | ||
value={{ | ||
sidebar: { showOperations: 'byDefault', showServers: 'byDefault' }, | ||
}} | ||
> | ||
<SpecificationContext.Provider value={parsed}> | ||
<Sidebar /> | ||
</SpecificationContext.Provider> | ||
</ConfigContext.Provider>, | ||
); | ||
}); | ||
}); |
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,47 @@ | ||
import { OperationInterface, TagV2, TagsV2 } from '@asyncapi/parser'; | ||
import { TagObject, filterObjectsByTags } from '../sidebar'; | ||
|
||
describe('sidebar', () => { | ||
describe('.filterObjectsByTags', () => { | ||
test('should handle empty objects and find nothing', () => { | ||
const tagsToFind = ['test']; | ||
const objects: Array<TagObject<OperationInterface>> = []; | ||
const filteredTags = filterObjectsByTags(tagsToFind, objects); | ||
expect(filteredTags.tagged.size).toEqual(0); | ||
expect(filteredTags.untagged.length).toEqual(0); | ||
}); | ||
test('should handle find one instance', () => { | ||
const tagsToFind = ['test']; | ||
const tagsToSearch = new TagsV2([new TagV2({ name: 'test' })]); | ||
const objects: Array<TagObject<any>> = [ | ||
{ data: {}, name: '', tags: tagsToSearch }, | ||
]; | ||
const filteredTags = filterObjectsByTags(tagsToFind, objects); | ||
expect(filteredTags.tagged.size).toEqual(1); | ||
expect(filteredTags.untagged.length).toEqual(0); | ||
}); | ||
test('should handle find multiple instances', () => { | ||
const tagsToFind = ['test']; | ||
const obj1 = { | ||
data: {}, | ||
name: '', | ||
tags: new TagsV2([new TagV2({ name: 'test' })]), | ||
}; | ||
const obj2 = { | ||
data: {}, | ||
name: '', | ||
tags: new TagsV2([new TagV2({ name: 'none' })]), | ||
}; | ||
const obj3 = { | ||
data: {}, | ||
name: '', | ||
tags: new TagsV2([new TagV2({ name: 'test' })]), | ||
}; | ||
const objects: Array<TagObject<any>> = [obj1, obj2, obj3]; | ||
const filteredTags = filterObjectsByTags(tagsToFind, objects); | ||
expect(filteredTags.tagged.size).toEqual(1); | ||
expect(filteredTags.tagged.get('test')!.length).toEqual(2); | ||
expect(filteredTags.untagged.length).toEqual(1); | ||
}); | ||
}); | ||
}); |
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,46 @@ | ||
import { TagsInterface } from '@asyncapi/parser'; | ||
|
||
export interface TagObject<T = any> { | ||
name: string; | ||
tags: TagsInterface; | ||
data: T; | ||
} | ||
export interface SortedReturnType { | ||
tagged: Map<string, TagObject[]>; | ||
untagged: TagObject[]; | ||
} | ||
|
||
/** | ||
* Filter an array of objects by certain tags | ||
*/ | ||
export function filterObjectsByTags<T>( | ||
tags: string[], | ||
objects: Array<TagObject<T>>, | ||
): SortedReturnType { | ||
const taggedObjects = new Set<TagObject>(); | ||
const tagged = new Map<string, TagObject[]>(); | ||
tags.forEach(tag => { | ||
const taggedForTag: TagObject[] = []; | ||
objects.forEach(obj => { | ||
const objTags = obj.tags; | ||
const nameTags = (objTags.all() ?? []).map(t => t.name()); | ||
const hasTag = nameTags.includes(tag); | ||
if (hasTag) { | ||
taggedForTag.push(obj); | ||
taggedObjects.add(obj); | ||
} | ||
}); | ||
if (taggedForTag.length > 0) { | ||
tagged.set(tag, taggedForTag); | ||
} | ||
}); | ||
|
||
const untagged: TagObject[] = []; | ||
objects.forEach(obj => { | ||
if (!taggedObjects.has(obj)) { | ||
untagged.push(obj); | ||
} | ||
}); | ||
|
||
return { tagged, untagged }; | ||
} |