@@ -16,6 +16,125 @@ jest.mock('algoliasearch-helper', () => {
1616 } ) ;
1717} ) ;
1818
19+ describe ( 'Usage' , ( ) => {
20+ it ( 'throws without indexName' , ( ) => {
21+ expect ( ( ) => {
22+ // eslint-disable-next-line no-new
23+ new InstantSearch ( { indexName : undefined } ) ;
24+ } ) . toThrowErrorMatchingInlineSnapshot ( `
25+ "The \`indexName\` option is required.
26+
27+ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsearch/js/"
28+ ` ) ;
29+ } ) ;
30+
31+ it ( 'throws without searchClient' , ( ) => {
32+ expect ( ( ) => {
33+ // eslint-disable-next-line no-new
34+ new InstantSearch ( { indexName : 'indexName' , searchClient : undefined } ) ;
35+ } ) . toThrowErrorMatchingInlineSnapshot ( `
36+ "The \`searchClient\` option is required.
37+
38+ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsearch/js/"
39+ ` ) ;
40+ } ) ;
41+
42+ it ( 'throws if searchClient does not implement a search method' , ( ) => {
43+ expect ( ( ) => {
44+ // eslint-disable-next-line no-new
45+ new InstantSearch ( { indexName : 'indexName' , searchClient : { } } ) ;
46+ } ) . toThrowErrorMatchingInlineSnapshot ( `
47+ "The \`searchClient\` must implement a \`search\` method.
48+
49+ See: https://www.algolia.com/doc/guides/building-search-ui/going-further/backend-search/in-depth/backend-instantsearch/js/"
50+ ` ) ;
51+ } ) ;
52+
53+ it ( 'throws if addWidgets is called with a single widget' , ( ) => {
54+ expect ( ( ) => {
55+ const search = new InstantSearch ( {
56+ indexName : 'indexName' ,
57+ searchClient : { search : ( ) => { } } ,
58+ } ) ;
59+ search . addWidgets ( { } ) ;
60+ } ) . toThrowErrorMatchingInlineSnapshot ( `
61+ "The \`addWidgets\` method expects an array of widgets. Please use \`addWidget\`.
62+
63+ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsearch/js/"
64+ ` ) ;
65+ } ) ;
66+
67+ it ( 'throws if a widget without render or init method is added' , ( ) => {
68+ const widgets = [ { render : undefined , init : undefined } ] ;
69+
70+ expect ( ( ) => {
71+ const search = new InstantSearch ( {
72+ indexName : 'indexName' ,
73+ searchClient : { search : ( ) => { } } ,
74+ } ) ;
75+ search . addWidgets ( widgets ) ;
76+ } ) . toThrowErrorMatchingInlineSnapshot ( `
77+ "The widget definition expects a \`render\` and/or an \`init\` method.
78+
79+ See: https://www.algolia.com/doc/guides/building-search-ui/widgets/create-your-own-widgets/js/"
80+ ` ) ;
81+ } ) ;
82+
83+ it ( 'does not throw with a widget having a init method' , ( ) => {
84+ const widgets = [ { init : ( ) => { } } ] ;
85+
86+ expect ( ( ) => {
87+ const search = new InstantSearch ( {
88+ indexName : 'indexName' ,
89+ searchClient : { search : ( ) => { } } ,
90+ } ) ;
91+ search . addWidgets ( widgets ) ;
92+ } ) . not . toThrow ( ) ;
93+ } ) ;
94+
95+ it ( 'does not throw with a widget having a render method' , ( ) => {
96+ const widgets = [ { render : ( ) => { } } ] ;
97+
98+ expect ( ( ) => {
99+ const search = new InstantSearch ( {
100+ indexName : 'indexName' ,
101+ searchClient : { search : ( ) => { } } ,
102+ } ) ;
103+ search . addWidgets ( widgets ) ;
104+ } ) . not . toThrow ( ) ;
105+ } ) ;
106+
107+ it ( 'throws if removeWidgets is called with a single widget' , ( ) => {
108+ expect ( ( ) => {
109+ const search = new InstantSearch ( {
110+ indexName : 'indexName' ,
111+ searchClient : { search : ( ) => { } } ,
112+ } ) ;
113+ search . removeWidgets ( { } ) ;
114+ } ) . toThrowErrorMatchingInlineSnapshot ( `
115+ "The \`removeWidgets\` method expects an array of widgets. Please use \`removeWidget\`.
116+
117+ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsearch/js/"
118+ ` ) ;
119+ } ) ;
120+
121+ it ( 'throws if a widget without dispose method is removed' , ( ) => {
122+ const widgets = [ { init : ( ) => { } , render : ( ) => { } , dispose : undefined } ] ;
123+
124+ expect ( ( ) => {
125+ const search = new InstantSearch ( {
126+ indexName : 'indexName' ,
127+ searchClient : { search : ( ) => { } } ,
128+ } ) ;
129+ search . removeWidgets ( widgets ) ;
130+ } ) . toThrowErrorMatchingInlineSnapshot ( `
131+ "The \`dispose\` method is required to remove the widget.
132+
133+ See: https://www.algolia.com/doc/guides/building-search-ui/widgets/create-your-own-widgets/js/#the-widget-lifecycle-and-api"
134+ ` ) ;
135+ } ) ;
136+ } ) ;
137+
19138describe ( 'InstantSearch lifecycle' , ( ) => {
20139 let algoliasearch ;
21140 let client ;
@@ -67,20 +186,6 @@ describe('InstantSearch lifecycle', () => {
67186 expect ( algoliaSearchHelper ) . not . toHaveBeenCalled ( ) ;
68187 } ) ;
69188
70- describe ( 'when adding a widget without render and init' , ( ) => {
71- let widget ;
72-
73- beforeEach ( ( ) => {
74- widget = { } ;
75- } ) ;
76-
77- it ( 'throw an error' , ( ) => {
78- expect ( ( ) => {
79- search . addWidget ( widget ) ;
80- } ) . toThrow ( 'Widget definition missing render or init method' ) ;
81- } ) ;
82- } ) ;
83-
84189 it ( 'does not fail when passing same references inside multiple searchParameters props' , ( ) => {
85190 const disjunctiveFacetsRefinements = { fruits : [ 'apple' ] } ;
86191 const facetsRefinements = disjunctiveFacetsRefinements ;
@@ -663,7 +768,9 @@ it('Does not allow to start twice', () => {
663768 expect ( ( ) => instance . start ( ) ) . not . toThrow ( ) ;
664769 expect ( ( ) => {
665770 instance . start ( ) ;
666- } ) . toThrowErrorMatchingInlineSnapshot (
667- `"start() has been already called once"`
668- ) ;
771+ } ) . toThrowErrorMatchingInlineSnapshot ( `
772+ "The \`start\` method has already been called once.
773+
774+ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsearch/js/"
775+ ` ) ;
669776} ) ;
0 commit comments