Updating tests - state seems broken, needs attention

This commit is contained in:
James Barnsley
2020-10-28 21:24:38 +13:00
parent c511b763fb
commit 6da4181fa7
7 changed files with 256 additions and 257 deletions

View File

@ -1,34 +1,43 @@
import React from 'react';
import { BrowserRouter } from "react-router-dom";
// Testing-specific
import { shallow, mount, render } from 'enzyme';
const state = require('../state');
// Test subjects
import GridItem from '../../src/js/components/GridItem';
import { shallow } from 'enzyme';
import { GridItem } from '../../src/js/components/GridItem';
describe('<GridItem />', () => {
it('should handle album', () => {
var album = state.core.albums['jest:album:one'];
var dom = shallow(<GridItem item={album} />);
expect(dom.find('.grid__item__name').text()).toEqual('One');
expect(dom.find('.grid__item__secondary__content').length).toBe(1);
});
it('should handle album', () => {
const item = {
uri: 'spotify:album:alpha',
name: 'One',
};
const dom = shallow(<GridItem item={item} />);
expect(dom.find('.grid__item__name').text()).toEqual('One');
expect(dom.find('.grid__item__secondary__content').length).toBe(1);
});
it('should handle artist', () => {
var artist = state.core.artists['jest:artist:alpha'];
var dom = shallow(<GridItem item={artist} />);
expect(dom.find('.grid__item__name').text()).toEqual('Alpha');
expect(dom.find('.grid__item__secondary__content').childAt(0).render().text()).toEqual('123 followers');
expect(dom.find('.grid__item__secondary__content').childAt(1).render().text()).toEqual('1 albums');
});
it('should handle artist', () => {
const item = {
uri: 'spotify:artist:alpha',
name: 'Alpha',
followers: 123,
albums_uris: ['spotify:album:beta'],
};
const dom = shallow(<GridItem item={item} />);
expect(dom.find('.grid__item__name').text()).toEqual('Alpha');
expect(dom.find('.grid__item__secondary__content').childAt(0).render().text()).toEqual('123 followers');
expect(dom.find('.grid__item__secondary__content').childAt(1).render().text()).toEqual('1 albums');
});
it('should handle playlist', () => {
var playlist = state.core.playlists['jest:playlist:one'];
var dom = shallow(<GridItem item={playlist} />);
expect(dom.find('.grid__item__name').text()).toEqual('One');
expect(dom.find('.grid__item__secondary__content').render().text()).toEqual('2 tracks');
});
it('should handle playlist', () => {
const item = {
uri: 'spotify:playlist:alpha',
name: 'One',
tracks: [
{ uri: 'spotify:track:123' },
{ uri: 'spotify:track:456' },
],
};
const dom = shallow(<GridItem item={item} />);
expect(dom.find('.grid__item__name').text()).toEqual('One');
expect(dom.find('.grid__item__secondary__content').render().text()).toEqual('2 tracks');
});
});

View File

@ -1,236 +1,204 @@
import {
arrays,
storage,
format,
helpers,
arrays,
storage,
format,
helpers,
} from '../../src/js/util';
import { isLoading } from '../../src/js/util/helpers';
const state = require('../state');
describe('isLoading', () => {
let load_queue = {
'spotify:playlist:123': 'Some load queue value',
'stuff:and_things': 'stuff:and_things',
}
const load_queue = {
'spotify:playlist:123': 'Some load queue value',
'stuff:and_things': 'stuff:and_things',
};
it('should return false when not in load_queue', () => {
expect(isLoading(load_queue, ['not_there'])).toBe(false);
});
it('should return false when not in load_queue', () => {
expect(isLoading(load_queue, ['not_there'])).toBe(false);
});
it('should return true when in load queue', () => {
expect(isLoading(load_queue, ['stuff:and_things'])).toBe(true);
});
it('should return true when in load queue', () => {
expect(isLoading(load_queue, ['stuff:and_things'])).toBe(true);
});
it('should return true when in load queue (regex)', () => {
expect(isLoading(load_queue, ['(.*)playlist(.*)'])).toBe(true);
});
it('should return true when in load queue (regex)', () => {
expect(isLoading(load_queue, ['(.*)playlist(.*)'])).toBe(true);
});
it('should return false when regex is invalid', () => {
expect(isLoading(load_queue, ['(*.)playlist(*.)'])).toBe(false);
});
});
// TODO: DELETE THIS, WE USE LOCALFORAGE NOW
describe('localStorage', () => {
it('should handle keys that are not in storage', () => {
expect(storage.get('invalid_key')).toEqual({});
expect(storage.get('invalid_key', 'default_value')).toEqual('default_value');
});
it('should store data', () => {
// Initially empty
expect(storage.get('test_key')).toEqual({});
// Set it
storage.set('test_key', 'test_value');
// Test storage
expect(storage.get('test_key')).toEqual('test_value');
});
it('should return false when regex is invalid', () => {
expect(isLoading(load_queue, ['(*.)playlist(*.)'])).toBe(false);
});
});
describe('isCached', () => {
it('should return false when not cached', () => {
expect(storage.isCached('https://picsum.photos/200')).toBe(false);
});
it('should return false when not cached', () => {
expect(storage.isCached('https://picsum.photos/200')).toBe(false);
});
it('should return true when cached', () => {
var image = new Image();
image.src = 'https://picsum.photos/200';
image.onload = function(){
expect(storage.isCached('https://picsum.photos/200')).toBe(true);
}
});
it('should return true when cached', () => {
const image = new Image();
image.src = 'https://picsum.photos/200';
image.onload = () => {
expect(storage.isCached('https://picsum.photos/200')).toBe(true);
};
});
});
describe('formatImages', () => {
it('should ignore already-formatted objects', () => {
var images = [
{
formatted: true,
small: 'ignored-image.jpg'
}
];
expect(format.formatImages(images).small).toBe('ignored-image.jpg');
});
it('should handle Mopidy object', () => {
var images = [
{
__model__: 'Image',
width: 600,
url: 'test-image.jpg'
}
];
expect(format.formatImages(images).small).toBe('test-image.jpg');
});
it('should handle Mopidy string', () => {
var images = ['test-image.jpg'];
expect(format.formatImages(images).small).toBe('test-image.jpg');
});
it('should handle Spotify image', () => {
var images = [
{
width: 600,
url: 'test-image.jpg'
}
];
expect(format.formatImages(images).small).toBe('test-image.jpg');
});
it('should handle LastFM image', () => {
var images = [
{
size: 'small',
'#text': 'test-image.jpg'
}
];
expect(format.formatImages(images).small).toBe('test-image.jpg');
});
it('should handle Genius image', () => {
var images = {
small: {
url: 'test-image.jpg'
}
};
expect(format.formatImages(images).small).toBe('test-image.jpg');
});
it('should up-fill sizes', () => {
var images = [
{
width: 50,
url: 'small.jpg'
}
];
expect(format.formatImages(images).medium).toBe('small.jpg');
expect(format.formatImages(images).large).toBe('small.jpg');
expect(format.formatImages(images).huge).toBe('small.jpg');
});
it('should down-fill sizes', () => {
var images = [
{
width: 1900,
url: 'huge.jpg'
}
];
expect(format.formatImages(images).small).toBe('huge.jpg');
expect(format.formatImages(images).medium).toBe('huge.jpg');
expect(format.formatImages(images).large).toBe('huge.jpg');
});
});
it('should ignore already-formatted objects', () => {
let images = [
{
formatted: true,
small: 'ignored-image.jpg',
},
];
expect(format.formatImages(images).small).toBe('ignored-image.jpg');
});
it('should handle Mopidy object', () => {
let images = [
{
__model__: 'Image',
width: 600,
url: 'test-image.jpg',
},
];
expect(format.formatImages(images).small).toBe('test-image.jpg');
});
it('should handle Mopidy string', () => {
let images = ['test-image.jpg'];
expect(format.formatImages(images).small).toBe('test-image.jpg');
});
it('should handle Spotify image', () => {
let images = [
{
width: 600,
url: 'test-image.jpg',
},
];
expect(format.formatImages(images).small).toBe('test-image.jpg');
});
it('should handle LastFM image', () => {
let images = [
{
size: 'small',
'#text': 'test-image.jpg',
},
];
expect(format.formatImages(images).small).toBe('test-image.jpg');
});
it('should handle Genius image', () => {
let images = {
small: {
url: 'test-image.jpg',
},
};
expect(format.formatImages(images).small).toBe('test-image.jpg');
});
it('should up-fill sizes', () => {
let images = [
{
width: 50,
url: 'small.jpg',
},
];
expect(format.formatImages(images).medium).toBe('small.jpg');
expect(format.formatImages(images).large).toBe('small.jpg');
expect(format.formatImages(images).huge).toBe('small.jpg');
});
it('should down-fill sizes', () => {
let images = [
{
width: 1900,
url: 'huge.jpg',
},
];
expect(format.formatImages(images).small).toBe('huge.jpg');
expect(format.formatImages(images).medium).toBe('huge.jpg');
expect(format.formatImages(images).large).toBe('huge.jpg');
});
});
/**
* TODO: Formatters
**/
* */
describe('uriSource', () => {
it('should digest uri into a string', () => {
expect(typeof(helpers.uriType('spotify:album:123'))).toBe('string');
expect(helpers.uriSource('spotify:album:123')).toBe('spotify');
});
describe('uriSource', () => {
it('should digest uri into a string', () => {
expect(typeof (helpers.uriType('spotify:album:123'))).toBe('string');
expect(helpers.uriSource('spotify:album:123')).toBe('spotify');
});
});
describe('uriType', () => {
it('should digest uri into a string', () => {
expect(typeof(helpers.uriType('spotify:album:123'))).toBe('string');
expect(helpers.uriType('spotify:album:123')).toBe('album');
});
describe('uriType', () => {
it('should digest uri into a string', () => {
expect(typeof (helpers.uriType('spotify:album:123'))).toBe('string');
expect(helpers.uriType('spotify:album:123')).toBe('album');
});
});
describe('sourceIcon', () => {
it('should digest uri into a string', () => {
expect(typeof(helpers.sourceIcon('spotify:album:123'))).toBe('string');
expect(helpers.sourceIcon('spotify:album:123')).toBe('spotify');
});
describe('sourceIcon', () => {
it('should digest uri into a string', () => {
expect(typeof (helpers.sourceIcon('spotify:album:123'))).toBe('string');
expect(helpers.sourceIcon('spotify:album:123')).toBe('spotify');
});
});
describe('buildLink', () => {
it('should build uri into link as a string', () => {
let link = helpers.buildLink('spotify:album:123');
expect(typeof (link)).toBe('string');
expect(link).toBe('/album/spotify%3Aalbum%3A123');
});
it('should build uri into link as a string', () => {
var link = helpers.buildLink('spotify:album:123');
expect(typeof(link)).toBe('string');
expect(link).toBe('/album/spotify%3Aalbum%3A123');
});
it('should handle special characters', () => {
var link = helpers.buildLink('spotify:album:http://test.com/123!@#$%^&[];<>/?" .mp3');
expect(typeof(link)).toBe('string');
expect(link).toBe('/album/spotify%3Aalbum%3Ahttp%3A%2F%2Ftest.com%2F123!%40%23%24%25%5E%26%5B%5D%3B%3C%3E%2F%3F%22%20.mp3');
});
it('should handle special characters', () => {
let link = helpers.buildLink('spotify:album:http://test.com/123!@#$%^&[];<>/?" .mp3');
expect(typeof (link)).toBe('string');
expect(link).toBe('/album/spotify%3Aalbum%3Ahttp%3A%2F%2Ftest.com%2F123!%40%23%24%25%5E%26%5B%5D%3B%3C%3E%2F%3F%22%20.mp3');
});
});
describe('arrayOf', () => {
it('should return a one-dimensional array', () => {
const items = [
{
uri: '123',
name: '123',
},
{
uri: '456',
name: '456',
},
];
const uris = arrays.arrayOf('uri', items);
expect(Array.isArray(uris)).toBe(true);
expect(uris.length).toBe(2);
it('should return a one-dimensional array', () => {
var items = [
{
uri: '123',
name: '123'
},
{
uri: '456',
name: '456'
}
];
var uris = arrays.arrayOf('uri', items);
expect(Array.isArray(uris)).toBe(true);
expect(uris.length).toBe(2);
for (let uri of uris) {
expect(typeof (uri)).toBe('string');
}
});
for (var uri of uris){
expect(typeof(uri)).toBe('string');
}
});
it('should remove null and undefined items', () => {
var items = [
{
uri: '123',
name: '123'
},
{
uri: null,
name: '456'
},
{
name: '789'
}
];
var uris = arrays.arrayOf('uri', items);
expect(uris.length).toBe(1);
});
});
it('should remove null and undefined items', () => {
let items = [
{
uri: '123',
name: '123',
},
{
uri: null,
name: '456',
},
{
name: '789',
},
];
let uris = arrays.arrayOf('uri', items);
expect(uris.length).toBe(1);
});
});

View File

@ -1,33 +1,39 @@
import React from 'react';
import { shallow } from 'enzyme';
// Testing-specific
import { shallow, mount, render } from 'enzyme';
const state = require('../state');
// Test subjects
import { Album } from '../../src/js/views/Album';
import * as uiActions from '../../src/js/services/ui/actions';
import * as coreActions from '../../src/js/services/core/actions';
const state = require('../state');
describe('<Album />', () => {
const album = {
uri: 'jest:album:one',
name: 'One',
artists_uris: [
'jest:artist:alpha',
],
tracks_uris: [
'jest:track:one',
'jest:track:two',
],
wiki: 'Wiki text',
};
var album = state.core.albums['jest:album:one'];
it('should render accurately', () => {
const dom = shallow(
<Album
it('should render accurately', () => {
const dom = shallow(
<Album
album={album}
uiActions={uiActions}
coreActions={coreActions}
/>,
{
disableLifecycleMethods: true,
disableLifecycleMethods: true,
},
);
);
expect(dom.find('.album-view').length).toBe(1);
expect(dom.find('h1').text()).toEqual('One');
expect(dom.find('.wiki__text p').text()).toEqual('Wiki text');
});
});
expect(dom.find('.album-view').length).toBe(1);
expect(dom.find('h1').text()).toEqual('One');
expect(dom.find('.wiki__text p').text()).toEqual('Wiki text');
});
});