media_player_wrapper
Version:
A React Native wrapper for live audio streaming.
163 lines (128 loc) • 3.67 kB
JavaScript
import jsdom from "jsdom";
import "jsdom-global/register"; //at the top of file , even , before importing react
import "react-native";
import React from "react";
import { connect, Provider } from "react-redux";
// Note: test renderer must be required after react-native.
import renderer from "react-test-renderer";
import * as actions from "../app/actions";
import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
import reducer from "../app/reducers";
import { shollow, mount } from "enzyme";
var Enzyme = require("enzyme");
import configureStore from "../app/store/configureStore";
const store = configureStore();
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
jest.mock("MediaLibraray");
import Streaming from "../app/Streaming.js";
// React 16 Enzyme adapter
import Adapter from "enzyme-adapter-react-16";
Enzyme.configure({ adapter: new Adapter() });
// Testing Actions
describe("actions", () => {
it("should test the playbackState Action", () => {
let playbackState = "playing";
const playbackStateAction = {
type: "SET_PLAYBACK_STATE",
playbackState
};
expect(actions.setPlaybackState(playbackState)).toEqual(
playbackStateAction
);
});
it("it should test setVolume Action", () => {
let volume = 1;
const setVolumeAction = {
type: "SET_VOLUME",
volume
};
expect(actions.setVolume(volume)).toEqual(setVolumeAction);
});
it("should test the setIsPlaying Action", () => {
let value = true;
const isPlayingAction = {
type: "SET_IS_PLAYING",
value
};
expect(actions.setIsPlaying(value)).toEqual(isPlayingAction);
});
});
// Testing Reducers
describe("reducers", () => {
it("should return the initial state", () => {
let state = {
playbackState: "Connecting",
isPlaying: false,
volume: 0.0
};
expect(reducer(undefined, {})).toEqual(state);
});
it("should handle setVolume correcty", () => {
let action = {
type: "SET_VOLUME",
volume: 1
};
let initialState = {
playbackState: "Connecting",
isPlaying: false,
volume: 0
};
let state = {
playbackState: "Connecting",
isPlaying: false,
volume: 1
};
expect(reducer(initialState, action)).toEqual(state);
});
it("should handle setPlaybackState reducer", () => {
let action = {
type: "SET_PLAYBACK_STATE",
playbackState: "playing"
};
let initialState = {
playbackState: "Connecting",
isPlaying: false,
volume: 0
};
let state = {
playbackState: "playing",
isPlaying: false,
volume: 0
};
expect(reducer(initialState, action)).toEqual(state);
});
});
// Testing the components
describe("components", () => {
function setup() {
const props = {
playbackState: jest.fn(),
isPlaying: jest.fn(),
volume: jest.fn()
};
const store = mockStore({
playbackState: jest.fn(),
isPlaying: jest.fn(),
volume: jest.fn()
});
const enzymeWrapper = mount(<Streaming {...props} store={store} />);
return {
props,
enzymeWrapper
};
}
it("renders correctly", () => {
// Save a snapshot from the rendered component and make sure no changes happen
const store = mockStore({
playbackState: "playing",
isPlaying: false,
volume: 0,
nowPlayingReducer: jest.fn()
});
const wrapper = Enzyme.shallow(<Streaming store={store} />);
expect(wrapper.find("TouchableHighlight")).toBeDefined();
expect(wrapper).toMatchSnapshot();
});
});