scratch-storage/test/unit/load-default-assets.js
Christopher Willis-Ford 2b28f27049 Add tests, fix problems found by those tests
Changes include:
- Use `bin-loader` instead of `arraybuffer-loader` since the latter is
  only compatible with browsers, not with Node itself.
- Use `got` instead of `xhr` since `xhr` only works in browsers and
  `got` is nicer anyway.
- Add `json-loader` to Webpack config since `got` requires it.
- Use lower-case values in `DataFormat` to match canonical file names.
- Update `ScratchStorage.addWebSource` to match `WebHelper`'s new API.
- Fix an error in the web helper which was causing an infinite loop on
  error.
2016-12-08 16:45:56 -08:00

51 lines
1.4 KiB
JavaScript

const crypto = require('crypto');
const test = require('tap').test;
const ScratchStorage = require('../../dist/node/scratch-storage');
const {Asset, AssetType} = ScratchStorage;
const defaultAssetTypes = [AssetType.ImageBitmap, AssetType.ImageVector, AssetType.Sound];
const defaultIds = {};
let storage;
test('constructor', t => {
storage = new ScratchStorage();
t.type(storage, ScratchStorage);
t.end();
});
test('getDefaultAssetId', t => {
for (let i = 0; i < defaultAssetTypes.length; ++i) {
const assetType = defaultAssetTypes[i];
const id = storage.getDefaultAssetId(assetType);
t.type(id, 'string');
defaultIds[assetType.name] = id;
}
t.end();
});
test('load', t => {
const promises = [];
for (let i = 0; i < defaultAssetTypes.length; ++i) {
const assetType = defaultAssetTypes[i];
const id = defaultIds[assetType.name];
const promise = storage.load(assetType, id);
t.type(promise, 'Promise');
promises.push(promise);
promise.then(asset => {
t.type(asset, Asset);
t.strictEqual(asset.assetId, id);
t.strictEqual(asset.assetType, assetType);
t.ok(asset.data.length);
const hash = crypto.createHash('md5');
hash.update(asset.data);
t.strictEqual(hash.digest('hex'), id);
});
}
return Promise.all(promises);
});