UNPKG

1.85 kBJavaScriptView Raw
1'use strict';
2
3const { AssertionError } = require('assert');
4const Fs = require('fs');
5const Path = require('path');
6const { expect } = require('code');
7const Lab = require('lab');
8const StandIn = require('stand-in');
9const CloudApi = require('../lib/cloudapi');
10
11
12const lab = exports.lab = Lab.script();
13const { describe, it } = lab;
14
15describe('cloudapi', () => {
16 const key = Fs.readFileSync(Path.join(__dirname, 'test.key'));
17 const log = () => {};
18
19 it('throws when missing a token in production', () => {
20 const env = process.env.NODE_ENV;
21 process.env.NODE_ENV = 'production';
22 expect(() => { return new CloudApi({ key, log }); }).to.throw(AssertionError);
23 process.env.NODE_ENV = env;
24 });
25
26 it('won\'t throw when missing a token in development', () => {
27 const env = process.env.NODE_ENV;
28 process.env.NODE_ENV = 'development';
29 expect(() => { return new CloudApi({ key, log }); }).to.not.throw();
30 process.env.NODE_ENV = env;
31 });
32
33 it('won\'t throw when missing a token in test', () => {
34 const env = process.env.NODE_ENV;
35 process.env.NODE_ENV = 'test';
36 expect(() => { return new CloudApi({ key, log }); }).to.not.throw();
37 process.env.NODE_ENV = env;
38 });
39
40 it('won\'t throw when there is a token', () => {
41 expect(() => { return new CloudApi({ key, log, token: 'blah' }); }).to.not.throw();
42 });
43
44 describe('fetch', () => {
45 it('caches GET responses', async () => {
46 const cloudapi = new CloudApi({ key, log, token: 'blah' });
47 StandIn.replaceOnce(cloudapi._wreck, 'get', (stand) => {
48 return {
49 res: {},
50 payload: {
51 foo: 'bar'
52 }
53 };
54 });
55
56 const results1 = await cloudapi.fetch('/test');
57 const results2 = await cloudapi.fetch('/test');
58 expect(results1.foo).to.equal(results2.foo);
59 });
60 });
61});