Skip to content

Commit

Permalink
feat(catalog): Add new catalog.hasComponent function
Browse files Browse the repository at this point in the history
Add new catalog.hasComponent function, returning true/false if component exists or not
  • Loading branch information
natterstefan committed May 13, 2019
1 parent 1156a4f commit c4c10a2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 46 deletions.
4 changes: 4 additions & 0 deletions src/components/catalog-provider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('CatalogProvider', () => {
TestComponent,
},
getComponent: expect.any(Function),
hasComponent: expect.any(Function),
},
}

Expand Down Expand Up @@ -120,6 +121,7 @@ describe('CatalogProvider', () => {
Title,
},
getComponent: expect.any(Function),
hasComponent: expect.any(Function),
}

const Consumer = () => {
Expand Down Expand Up @@ -153,6 +155,7 @@ describe('CatalogProvider', () => {
_TestComponent: TestComponent,
},
getComponent: expect.any(Function),
hasComponent: expect.any(Function),
}

mount(
Expand Down Expand Up @@ -184,6 +187,7 @@ describe('CatalogProvider', () => {
TestComponent,
},
getComponent: expect.any(Function),
hasComponent: expect.any(Function),
}

const Consumer = () => {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ class Catalog {
}

// get a component by id, if not available we return null
getComponent = id => get(this._components, id) || null
getComponent = component => get(this._components, component) || null

// returns a boolean value after checking if the component exists in the catalog
hasComponent = component => !!get(this._components, component)
}

export default Catalog
110 changes: 65 additions & 45 deletions src/lib/catalog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,64 +49,84 @@ describe('Catalog', () => {
TestComponent,
},
getComponent: expect.any(Function),
hasComponent: expect.any(Function),
})
})

it('returns null for a requested component, when it was created with an empty component catalog', () => {
// first we create an empty registry
testCatalog = new Catalog()

// eslint-disable-next-line jest/prefer-strict-equal
expect(testCatalog).toEqual({
_components: {},
getComponent: expect.any(Function),
describe('getComponent', () => {
it('returns null for a requested component, when it was created with an empty component catalog', () => {
// first we create an empty registry
testCatalog = new Catalog()

// eslint-disable-next-line jest/prefer-strict-equal
expect(testCatalog).toEqual({
_components: {},
getComponent: expect.any(Function),
hasComponent: expect.any(Function),
})

// now request a component from the catalog
const TestComponentFromCatalog = testCatalog.getComponent('TestComponent')
expect(TestComponentFromCatalog).toBeNull()
})

// now request a component from the catalog
const TestComponentFromCatalog = testCatalog.getComponent('TestComponent')
expect(TestComponentFromCatalog).toBeNull()
})
it('returns requested component fully functional', () => {
const TestComponentFromCatalog = testCatalog.getComponent('TestComponent')
const wrapper = shallow(<TestComponentFromCatalog />)
expect(wrapper.text()).toStrictEqual('Hello World')
})

it('returns requested component fully functional', () => {
const TestComponentFromCatalog = testCatalog.getComponent('TestComponent')
const wrapper = shallow(<TestComponentFromCatalog />)
expect(wrapper.text()).toStrictEqual('Hello World')
})
it('does not manipulate props of returned component', () => {
const clickSpy = jest.fn()
const TestButton = () => (
<button type="button" onClick={clickSpy}>
Hello Button
</button>
)

testCatalog = new Catalog({
components: {
TestButton,
},
})

it('does not manipulate props of returned component', () => {
const clickSpy = jest.fn()
const TestButton = () => (
<button type="button" onClick={clickSpy}>
Hello Button
</button>
)
const TestButtonFromCatalog = testCatalog.getComponent('TestButton')
const wrapper = shallow(<TestButtonFromCatalog />)
expect(wrapper.text()).toStrictEqual('Hello Button')

testCatalog = new Catalog({
components: {
TestButton,
},
wrapper.simulate('click')
expect(clickSpy).toHaveBeenCalledTimes(1)
})

const TestButtonFromCatalog = testCatalog.getComponent('TestButton')
const wrapper = shallow(<TestButtonFromCatalog />)
expect(wrapper.text()).toStrictEqual('Hello Button')
it('returns nested requested component fully functional', () => {
const TestComponentFromCatalog = testCatalog.getComponent(
'ArticlePage.AudioArticle',
)
const wrapper = shallow(<TestComponentFromCatalog />)
expect(wrapper.text()).toStrictEqual('AudioArticle')
})

wrapper.simulate('click')
expect(clickSpy).toHaveBeenCalledTimes(1)
it('returns null when nested requested component is not available', () => {
const TestComponentFromCatalog = testCatalog.getComponent(
'ArticlePage.OtherArticle',
)
expect(TestComponentFromCatalog).toBeNull()
})
})

it('returns nested requested component fully functional', () => {
const TestComponentFromCatalog = testCatalog.getComponent(
'ArticlePage.AudioArticle',
)
const wrapper = shallow(<TestComponentFromCatalog />)
expect(wrapper.text()).toStrictEqual('AudioArticle')
})
describe('hasComponent', () => {
it('returns true when component exists', () => {
const hasAudioArticle = testCatalog.hasComponent(
'ArticlePage.AudioArticle',
)
expect(hasAudioArticle).toBeTruthy()
})

it('returns null when nested requested component is not available', () => {
const TestComponentFromCatalog = testCatalog.getComponent(
'ArticlePage.OtherArticle',
)
expect(TestComponentFromCatalog).toBeNull()
it('returns false when component exists', () => {
const hasOtherArticle = testCatalog.hasComponent(
'ArticlePage.OtherArticle',
)
expect(hasOtherArticle).toBeFalsy()
})
})
})

0 comments on commit c4c10a2

Please sign in to comment.