|
| 1 | +var imgur = require('../lib/imgur.js'), |
| 2 | + Q = require('q'); |
| 3 | + |
| 4 | +describe('SEARCH', function () { |
| 5 | + describe('search options validations', function () { |
| 6 | + test('should fail when query is not passed', function () { |
| 7 | + var errMsg = |
| 8 | + 'Search requires a query. Try searching with a query (e.g cats).'; |
| 9 | + expect(imgur.search()).rejects.toMatch(errMsg); |
| 10 | + }); |
| 11 | + |
| 12 | + test('should fail when query is passed a boolean', function () { |
| 13 | + var errMsg = 'You did not pass a string as a query.'; |
| 14 | + expect(imgur.search(true)).rejects.toMatch(errMsg); |
| 15 | + }); |
| 16 | + |
| 17 | + test('should fail when query is passed a number', function () { |
| 18 | + var errMsg = 'You did not pass a string as a query.'; |
| 19 | + expect(imgur.search(1)).rejects.toMatch(errMsg); |
| 20 | + }); |
| 21 | + |
| 22 | + test('should fail when query is passed a number', function () { |
| 23 | + var errMsg = 'You did not pass a string as a query.'; |
| 24 | + expect(imgur.search(1)).rejects.toMatch(errMsg); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe("delegates to _imgurRequest('search', ...)", function () { |
| 29 | + var mockResult = { |
| 30 | + data: [], |
| 31 | + params: { |
| 32 | + page: '1', |
| 33 | + dateRange: 'month', |
| 34 | + sort: 'viral', |
| 35 | + }, |
| 36 | + }; |
| 37 | + var payload = '/viral/month/1?q=meme'; |
| 38 | + var _imgurRequestBackup = imgur._imgurRequest; |
| 39 | + |
| 40 | + beforeEach(function () { |
| 41 | + var deferred = Q.defer(); |
| 42 | + imgur._imgurRequest = jest |
| 43 | + .fn() |
| 44 | + .mockImplementation(() => deferred.promise); |
| 45 | + deferred.resolve(mockResult); |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(function () { |
| 49 | + imgur._imgurRequest.mockClear(); |
| 50 | + imgur._imgurRequest = _imgurRequestBackup; |
| 51 | + }); |
| 52 | + |
| 53 | + it('should delegate', function () { |
| 54 | + var promise = imgur.search('meme', { |
| 55 | + sort: 'viral', |
| 56 | + dateRange: 'month', |
| 57 | + page: '1', |
| 58 | + }); |
| 59 | + |
| 60 | + expect(imgur._imgurRequest).toHaveBeenCalledWith('search', payload); |
| 61 | + expect(promise).resolves.toMatchObject(mockResult); |
| 62 | + }); |
| 63 | + }); |
| 64 | +}); |
0 commit comments