From ae4a8810f8427b779b270fbfc20c32104a5897a2 Mon Sep 17 00:00:00 2001 From: Pixelastic Date: Tue, 22 Dec 2015 21:46:10 +0100 Subject: [PATCH] test(getSuggestionTemplate): Add tests for the suggestion template --- src/lib/DocSearch.js | 4 +-- test/DocSearch-test.js | 75 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/lib/DocSearch.js b/src/lib/DocSearch.js index 7017b765..a1bdf050 100644 --- a/src/lib/DocSearch.js +++ b/src/lib/DocSearch.js @@ -54,7 +54,7 @@ class DocSearch { this.autocomplete = autocomplete(this.input, autocompleteOptions, [{ source: this.getAutocompleteSource(), templates: { - suggestion: this.getSuggestionTemplate(), + suggestion: DocSearch.getSuggestionTemplate(), footer: templates.footer } }]); @@ -154,7 +154,7 @@ class DocSearch { }); } - getSuggestionTemplate() { + static getSuggestionTemplate() { const template = Hogan.compile(templates.suggestion); return (suggestion) => { return template.render(suggestion); diff --git a/test/DocSearch-test.js b/test/DocSearch-test.js index 11b6a62a..7b3a9649 100644 --- a/test/DocSearch-test.js +++ b/test/DocSearch-test.js @@ -686,5 +686,80 @@ describe('DocSearch', () => { // Then expect(actual[0].text).toEqual('…lorem foo bar ipsum.'); }); + it('should add the anchor to the url if one is set', () => { + // Given + let input = [{ + hierarchy: { + lvl0: 'Ruby', + lvl1: 'API', + lvl2: null, + lvl3: null, + lvl4: null, + lvl5: null + }, + content: 'foo bar', + url: 'http://foo.bar/', + anchor: 'anchor' + }]; + + // When + let actual = DocSearch.formatHits(input); + + // Then + expect(actual[0].url).toEqual('http://foo.bar/#anchor'); + }); + }); + + describe('getSuggestionTemplate', () => { + beforeEach(() => { + let templates = { + suggestion: '
' + }; + DocSearch.__Rewire__('templates', templates); + }); + afterEach(() => { + DocSearch.__ResetDependency__('templates'); + }); + it('should return a function', () => { + // Given + + // When + let actual = DocSearch.getSuggestionTemplate(); + + // Then + expect(actual).toBeA('function'); + }); + describe('returned function', () => { + let Hogan; + let render; + beforeEach(() => { + render = sinon.spy(); + Hogan = { + compile: sinon.stub().returns({render}) + }; + DocSearch.__Rewire__('Hogan', Hogan); + }); + it('should compile the suggestion template', () => { + // Given + + // When + DocSearch.getSuggestionTemplate(); + + // Then + expect(Hogan.compile.calledOnce).toBe(true); + expect(Hogan.compile.calledWith('
')).toBe(true); + }); + it('should call render on a Hogan template', () => { + // Given + let actual = DocSearch.getSuggestionTemplate(); + + // When + actual('foo'); + + // Then + expect(render.calledOnce).toBe(true); + expect(render.calledWith('foo')).toBe(true); + }); + }); }); });