diff --git a/.travis.yml b/.travis.yml index 5df1d22..72628d3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,3 @@ language: node_js node_js: - "5.1.0" - - "4.2.2" diff --git a/app/__tests__/__snapshots__/api-tests.js.snap b/app/__tests__/__snapshots__/api-tests.js.snap new file mode 100644 index 0000000..417796f --- /dev/null +++ b/app/__tests__/__snapshots__/api-tests.js.snap @@ -0,0 +1,30 @@ +exports[`test REST apis calls the get api 1`] = ` +Array [ + Array [ + "/api/users/me/", + Object { + "credentials": "same-origin", + "headers": Object { + "Accept": "application/json", + }, + "method": "get", + }, + ], +] +`; + +exports[`test REST apis calls the post api 1`] = ` +Array [ + Array [ + "/logout/", + Object { + "credentials": "same-origin", + "headers": Object { + "Accept": "application/json", + "X-CSRFToken": "", + }, + "method": "post", + }, + ], +] +`; diff --git a/app/__tests__/api-tests.js b/app/__tests__/api-tests.js index d5fb929..4d2639e 100644 --- a/app/__tests__/api-tests.js +++ b/app/__tests__/api-tests.js @@ -1,49 +1,33 @@ "use strict"; -jest.autoMockOff(); jest.mock('fetch-on-rest'); +import api from '../api.js' describe('test REST apis', function () { - var api = require('app/api.js'); - afterEach(function() { + beforeEach(() => { + window.fetch.mockClear() + }) + + afterEach(() => { expect(api.getPending()).toEqual([]); }); - pit('calls the get api', function() { + it('calls the post api', function() { + api.setResponse('/logout/', JSON.stringify({})); + return api.logout().then(() => { + expect(window.fetch.mock.calls).toMatchSnapshot() + }); + }); + + it('calls the get api', function() { api.setResponse('/api/users/me/', JSON.stringify({foo: 'bar'})); return api.get(api.me).then(resp => { expect(resp).toEqual({foo: 'bar'}); - expect(window.fetch.mock.calls.length).toBe(1); - expect(window.fetch.mock.calls[0][0]).toEqual('/api/users/me/'); - var headers = { - credentials: 'same-origin', - headers: { - Accept: 'application/json' - }, - method: 'get' - }; - expect(window.fetch.mock.calls[0][1]).toEqual(headers); + expect(window.fetch.mock.calls).toMatchSnapshot() }) }); - pit('calls the post api', function() { - api.setResponse('/logout/', JSON.stringify({})); - return api.logout().then(() => { - expect(window.fetch.mock.calls.length).toBe(1); - expect(window.fetch.mock.calls[0][0]).toEqual('/logout/'); - var headers = { - credentials: 'same-origin', - headers: { - Accept: 'application/json', - 'X-CSRFToken': '' - }, - method: 'post' - }; - expect(window.fetch.mock.calls[0][1]).toEqual(headers); - }); - }); - - pit('calls the delete api', function() { + it('calls the delete api', function() { api.setResponse('/api/screens/33/?foo=bar', "{}"); return api.delete(['screens', 33], {foo: 'bar'}).then(() => { expect(window.fetch).toBeCalledWith( @@ -64,7 +48,6 @@ describe('test REST apis', function () { describe('test dependent libraries', function(){ it('checks expansions', function() { - var api = require('app/api.js'); expect(api._getUrl(api.me)).toEqual('/api/users/me/'); expect(api._getUrl(api.me, {})).toEqual('/api/users/me/'); expect(api._getUrl(api.me, {foo: 'bar'})).toEqual('/api/users/me/?foo=bar'); diff --git a/app/__tests__/watchlist-tests.js b/app/__tests__/watchlist-tests.js index 8c5c199..12ce42c 100644 --- a/app/__tests__/watchlist-tests.js +++ b/app/__tests__/watchlist-tests.js @@ -1,9 +1,13 @@ 'use strict'; -jest.autoMockOff(); -jest.mock('fetch-on-rest'); - - -var screen = { +jest.disableAutomock() +jest.mock('fetch-on-rest') +import api from '../api.js' +import React from 'react' +import ReactDOM from 'react-dom' +import Watchlist from '../watchlist.jsx' +import TestUtils from 'react-addons-test-utils' + +var SCREEN = { ratios: [[ "Current price", "CMP", @@ -15,19 +19,17 @@ var screen = { }; describe('watchlist Tests', function() { - var api = require('app/api.js'); - var watchlist, TestUtils; + var watchlist beforeEach(function() { - var React = require('react'); - var Watchlist = require('../watchlist.jsx'); - window.loggedIn = true; - TestUtils = require('react-addons-test-utils'); var params = { search: '', query: {}, pathname: '/watchlist/' }; + window.loggedIn = true; + api.setResponse('/api/users/watchlist/', + JSON.stringify(SCREEN)); watchlist = TestUtils.renderIntoDocument( ); @@ -37,21 +39,14 @@ describe('watchlist Tests', function() { expect(api.getPending()).toEqual([]); }); - it('should show loading', function() { - var ReactDOM = require('react-dom'); + it('should load watchlist', function() { var dom = ReactDOM.findDOMNode(watchlist); expect(dom.textContent).toEqual('Loading...'); - }); - - pit('should load watchlist', function() { - api.setResponse('/api/users/watchlist/', - JSON.stringify(screen)); - return watchlist.componentDidMount().then(() => { - expect(watchlist.state.screen).toEqual(screen); + return watchlist._req.then(() => { + expect(watchlist.state.screen).toEqual(SCREEN); var table = TestUtils.findRenderedDOMComponentWithTag( watchlist, 'table'); expect(table).toBeDefined(); }); }); - }); diff --git a/app/api.js b/app/api.js index b30ef84..cf1ca01 100644 --- a/app/api.js +++ b/app/api.js @@ -1,5 +1,5 @@ "use strict"; -var Rest = require('fetch-on-rest'); +import Rest from 'fetch-on-rest' function getCookie(sKey) { if (!sKey) { return ''; } @@ -54,4 +54,4 @@ Api.cid = function(cid, component) { return ['company', cid, component]; }; -module.exports = Api; +export default Api diff --git a/app/base.jsx b/app/base.jsx index d0cf535..a19dcc3 100644 --- a/app/base.jsx +++ b/app/base.jsx @@ -1,12 +1,11 @@ -"use strict"; -var React = require('react'); -var Link = require('react-router').Link; -var Nav = require('react-bootstrap/lib/Nav'); -var Navbar = require('react-bootstrap/lib/Navbar'); -var NavbarBrand = require('react-bootstrap/lib/NavbarBrand'); +import React from 'react' +import {Link} from 'react-router' +import Nav from 'react-bootstrap/lib/Nav' +import Navbar from 'react-bootstrap/lib/Navbar' +import NavbarBrand from 'react-bootstrap/lib/NavbarBrand' -var CompanySearch = require('./components/company.search.jsx'); -var Api = require('./api.js'); +import CompanySearch from './components/company.search.jsx' +import Api from './api.js' function Footer() { @@ -16,9 +15,8 @@ function Footer() { Made with in India.

- Navigation Links: Home + Navigation Links: Home | Screens - | Talks | Change Log | Dalal-Street Blog | @@ -108,14 +106,13 @@ class Navigation extends React.Component { -