atom-io-client-spec.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const AtomIoClient = require('../lib/atom-io-client');
  2. describe("AtomIoClient", function() {
  3. beforeEach(function() {
  4. return this.client = new AtomIoClient;
  5. });
  6. it("fetches avatar from cache if the network is unavailable", function() {
  7. spyOn(this.client, 'online').andReturn(false);
  8. spyOn(this.client, 'fetchAndCacheAvatar');
  9. expect(this.client.fetchAndCacheAvatar).not.toHaveBeenCalled();
  10. return this.client.avatar('test-user', function() {});
  11. });
  12. describe("request", function() {
  13. it("fetches api json from cache if the network is unavailable", function() {
  14. spyOn(this.client, 'online').andReturn(false);
  15. spyOn(this.client, 'fetchFromCache').andReturn({});
  16. spyOn(this.client, 'request');
  17. this.client.package('test-package', function() {});
  18. expect(this.client.fetchFromCache).toHaveBeenCalled();
  19. expect(this.client.request).not.toHaveBeenCalled();
  20. });
  21. it("returns an error if the API response is not JSON", function() {
  22. const jsonParse = JSON.parse;
  23. waitsFor(function(done) {
  24. spyOn(this.client, 'parseJSON').andThrow();
  25. return this.client.request('path', function(error, data) {
  26. expect(error).not.toBeNull();
  27. return done();
  28. });
  29. });
  30. return runs(() => // Tests will throw without this as cleanup requires JSON.parse to work
  31. JSON.parse = jsonParse);
  32. });
  33. });
  34. it("handles glob errors", function() {
  35. spyOn(this.client, 'avatarGlob').andReturn(`${__dirname}/**`);
  36. spyOn(require('fs'), 'readdir').andCallFake((dirPath, callback) => process.nextTick(() => callback(new Error('readdir error'))));
  37. const callback = jasmine.createSpy('cacheAvatar callback');
  38. this.client.cachedAvatar('fakeperson', callback);
  39. waitsFor(() => callback.callCount === 1);
  40. return runs(() => expect(callback.argsForCall[0][0].message).toBe('readdir error'));
  41. });
  42. return xit("purges old items from cache correctly");
  43. });
  44. // "correctly" in this case means "remove all old items but one" so that we
  45. // always have stale data to return if the network is gone.