|
| 1 | +import chai from 'chai'; |
| 2 | +import chaiAsPromised from 'chai-as-promised'; |
| 3 | +import fetchMock from 'fetch-mock'; |
| 4 | +import Client from '../Client'; |
| 5 | +import Asset from './Asset'; |
| 6 | +import { assets as mockAssets } from '../mocks'; |
| 7 | + |
| 8 | +chai.should(); |
| 9 | +chai.use(chaiAsPromised); |
| 10 | + |
| 11 | +describe('When instantiating an asset based on customer and ID', () => { |
| 12 | + const client = new Client(); |
| 13 | + const asset = new Asset(client, Asset.makeHref('SYNC', 1)); |
| 14 | + |
| 15 | + it('should set the href', () => asset.href.should.equal('/1/SYNC/assets/1')); |
| 16 | + it('should not be hydrated', () => asset.hydrated.should.equal(false)); |
| 17 | +}); |
| 18 | + |
| 19 | +describe('When instantiating an asset based on an object', () => { |
| 20 | + const client = new Client(); |
| 21 | + const asset = new Asset(client, mockAssets.getById(1)); |
| 22 | + |
| 23 | + it('should set the ID', () => asset.id.should.equal(1)); |
| 24 | + it('should set the href', () => asset.href.should.equal('/1/SYNC/assets/1')); |
| 25 | + it('should be hydrated', () => asset.hydrated.should.equal(true)); |
| 26 | +}); |
| 27 | + |
| 28 | +describe('When fetching an asset based on customer and ID', () => { |
| 29 | + const client = new Client(); |
| 30 | + |
| 31 | + beforeEach(() => mockAssets.setUpSuccessfulMock(client)); |
| 32 | + beforeEach(() => fetchMock.catch(503)); |
| 33 | + afterEach(fetchMock.restore); |
| 34 | + |
| 35 | + let promise; |
| 36 | + beforeEach(() => { |
| 37 | + promise = new Asset(client, Asset.makeHref('SYNC', 1)).fetch(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should resolve the promise', () => promise.should.be.fulfilled); |
| 41 | + it('should set the ID', () => promise.then(v => v.id).should.eventually.equal(1)); |
| 42 | + it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/assets/1')); |
| 43 | + it('should be hydrated', () => promise.then(v => v.hydrated).should.eventually.equal(true)); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('When creating an asset', () => { |
| 47 | + const client = new Client(); |
| 48 | + |
| 49 | + beforeEach(() => mockAssets.setUpSuccessfulMock(client)); |
| 50 | + beforeEach(() => fetchMock.catch(503)); |
| 51 | + afterEach(fetchMock.restore); |
| 52 | + |
| 53 | + let promise; |
| 54 | + beforeEach(() => { |
| 55 | + promise = new Asset(client, { code: 'SYNC', base64_data: 'some_data' }).create(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should resolve the promise', () => promise.should.be.fulfilled); |
| 59 | + it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/assets/1')); |
| 60 | + it('should set the ID', () => promise.then(v => v.id).should.eventually.equal(1)); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('When updating an asset', () => { |
| 64 | + const client = new Client(); |
| 65 | + |
| 66 | + beforeEach(() => mockAssets.setUpSuccessfulMock(client)); |
| 67 | + beforeEach(() => fetchMock.catch(503)); |
| 68 | + afterEach(fetchMock.restore); |
| 69 | + |
| 70 | + let promise; |
| 71 | + beforeEach(() => { |
| 72 | + promise = new Asset(client, { code: 'SYNC' }) |
| 73 | + .create() |
| 74 | + .then(asset => asset.markSaved() |
| 75 | + .then(() => asset)); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should resolve the promise', () => promise.should.be.fulfilled); |
| 79 | + it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/assets/1')); |
| 80 | + it('should set is_saved to true', () => promise.then(v => v.is_saved).should.eventually.equal(true)); |
| 81 | +}); |
0 commit comments