Skip to content

Commit

Permalink
tests(productProjection): add tests for the search-endpoint process f…
Browse files Browse the repository at this point in the history
…unction
  • Loading branch information
philippspo committed Jan 28, 2016
1 parent 8597a61 commit 709e6a5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/coffee/services/product-projections.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ class ProductProjectionService extends BaseService
# .catch (error) ->
# # eg: 'BAD'
process: (fn, options = {}) ->
console.log @_currentEndpoint
if @_currentEndpoint != '/product-projections/search'
return super(fn, options)
throw new Error 'Please provide a function to process the elements' unless _.isFunction fn
Expand All @@ -372,6 +371,7 @@ class ProductProjectionService extends BaseService
perPage: perPage,
offset: (page - 1) * perPage
total: total

if total? and (page - 1) * perPage >= total
resolve acc
else
Expand All @@ -398,7 +398,6 @@ class ProductProjectionService extends BaseService
accumulated = acc.concat(result) if options.accumulate
_processPage nextPage, perPage, newTotal, accumulated
.catch (error) ->
console.log JSON.stringify error, null, 2
reject error
.done()
_processPage(@_params.query.page or 1, @_params.query.perPage or 20)
Expand Down
36 changes: 36 additions & 0 deletions src/spec/client/services/product-projections.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,39 @@ describe 'ProductProjectionService', ->
expect(result[2].endpoint).toMatch /\?sort=name%20desc&staged=true&withTotal=false&where=foo%3Dbar%20or%20hello%3Dworld%20and%20id%20%3E%20%22id_20%22$/
done()
.catch (err) -> done(_.prettify err)

it 'should call the product-projection-search endpoint
when using asSearch', (done) ->
offset = -20
count = 20
spyOn(@restMock, 'GET').andCallFake (endpoint, callback) ->
offset += 20
callback(null, {statusCode: 200}, {
total: 50
count: if offset is 40 then 10 else count
offset: offset
results: _.map (if offset is 40 then [1..10] else [1..20]), (i) -> {id: "id_#{i}", endpoint}

})
fn = (payload) ->
Promise.resolve payload.body.results[0]
@service
.asSearch()
.process(fn)
.then (results) ->
allEndpointsAreSearch = _.reduce(results,
(allSearchEndpoints, request) ->
endpointIsSearch =
request.endpoint.match /^\/product-projections\/search/
return allSearchEndpoints && !!endpointIsSearch
)
actual = allEndpointsAreSearch
expected = true
expect(actual).toEqual(expected)
done()
.catch (err) -> done(_.prettify err)

it 'should throw error if function is missing', ->
spyOn(@restMock, 'GET')
expect(=> @service.asSearch().process()).toThrow new Error 'Please provide a function to process the elements'
expect(@restMock.GET).not.toHaveBeenCalled()

0 comments on commit 709e6a5

Please sign in to comment.