diff --git a/src/short-urls/ShortUrlVisits.js b/src/short-urls/ShortUrlVisits.js index 2f8f15dc..3ec188bf 100644 --- a/src/short-urls/ShortUrlVisits.js +++ b/src/short-urls/ShortUrlVisits.js @@ -7,11 +7,15 @@ import Moment from 'react-moment' import { connect } from 'react-redux' import { Card, CardBody, CardHeader, UncontrolledTooltip } from 'reactstrap' import DateInput from '../common/DateInput' -import VisitsParser from '../visits/services/VisitsParser' +import visitsParser from '../visits/services/VisitsParser' import { getShortUrlVisits } from './reducers/shortUrlVisits' import './ShortUrlVisits.scss' import MutedMessage from '../utils/MuttedMessage'; +const defaultProps = { + visitsParser, +}; + export class ShortUrlsVisits extends React.Component { state = { startDate: undefined, endDate: undefined }; loadVisits = () => { @@ -167,8 +171,6 @@ export class ShortUrlsVisits extends React.Component { } } -ShortUrlsVisits.defaultProps = { - visitsParser: VisitsParser -}; +ShortUrlsVisits.defaultProps = defaultProps; export default connect(pick(['selectedServer', 'shortUrlVisits']), { getShortUrlVisits })(ShortUrlsVisits); diff --git a/src/visits/services/VisitsParser.js b/src/visits/services/VisitsParser.js index 2d35a760..a17691b7 100644 --- a/src/visits/services/VisitsParser.js +++ b/src/visits/services/VisitsParser.js @@ -21,14 +21,14 @@ const browserFromUserAgent = userAgent => { const lowerUserAgent = userAgent.toLowerCase(); switch (true) { + case (lowerUserAgent.indexOf('opera') >= 0 || lowerUserAgent.indexOf('opr') >= 0): + return 'Opera'; case (lowerUserAgent.indexOf('firefox') >= 0): return 'Firefox'; case (lowerUserAgent.indexOf('chrome') >= 0): return 'Chrome'; case (lowerUserAgent.indexOf('safari') >= 0): return 'Safari'; - case (lowerUserAgent.indexOf('opera') >= 0): - return 'Opera'; case (lowerUserAgent.indexOf('msie') >= 0): return 'Internet Explorer'; default: @@ -98,4 +98,5 @@ export class VisitsParser { }; } -export default new VisitsParser(); +const visitsParser = new VisitsParser(); +export default visitsParser; diff --git a/test/shortUrls/reducers/shortUrlsListParams.test.js b/test/short-urls/reducers/shortUrlsListParams.test.js similarity index 91% rename from test/shortUrls/reducers/shortUrlsListParams.test.js rename to test/short-urls/reducers/shortUrlsListParams.test.js index f22507d3..7540f673 100644 --- a/test/shortUrls/reducers/shortUrlsListParams.test.js +++ b/test/short-urls/reducers/shortUrlsListParams.test.js @@ -5,10 +5,10 @@ import reduce, { import { LIST_SHORT_URLS } from '../../../src/short-urls/reducers/shortUrlsList'; describe('shortUrlsListParamsReducer', () => { - describe('reduce', () => { + describe('reducer', () => { const defaultState = { page: '1' }; - it('returns default value when action is anknown', () => + it('returns default value when action is unknown', () => expect(reduce(defaultState, { type: 'unknown' })).toEqual(defaultState) ); diff --git a/test/visits/services/VisitsParser.test.js b/test/visits/services/VisitsParser.test.js new file mode 100644 index 00000000..e2141c73 --- /dev/null +++ b/test/visits/services/VisitsParser.test.js @@ -0,0 +1,76 @@ +import visitsParser from '../../../src/visits/services/VisitsParser'; + +describe('VisitsParser', () => { + const visits = [ + { + userAgent: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0', + referer: 'https://google.com', + visitLocation: { + countryName: 'Spain', + }, + }, + { + userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0', + referer: 'https://google.com', + visitLocation: { + countryName: 'United States', + }, + }, + { + userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', + visitLocation: { + countryName: 'Spain', + }, + }, + { + userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', + referer: 'https://m.facebook.com', + visitLocation: { + countryName: 'Spain', + }, + }, + { + userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41', + }, + ]; + + describe('processOsStats', () => { + it('properly parses OS stats', () => { + expect(visitsParser.processOsStats(visits)).toEqual({ + 'Linux': 3, + 'Windows': 1, + 'MacOS': 1, + }); + }); + }); + + describe('processBrowserStats', () => { + it('properly parses browser stats', () => { + expect(visitsParser.processBrowserStats(visits)).toEqual({ + 'Firefox': 2, + 'Chrome': 2, + 'Opera': 1, + }); + }); + }); + + describe('processReferrersStats', () => { + it('properly parses referrer stats', () => { + expect(visitsParser.processReferrersStats(visits)).toEqual({ + 'Unknown': 2, + 'google.com': 2, + 'm.facebook.com': 1, + }); + }); + }); + + describe('processCountriesStats', () => { + it('properly parses countries stats', () => { + expect(visitsParser.processCountriesStats(visits)).toEqual({ + 'Spain': 3, + 'United States': 1, + 'Unknown': 1, + }); + }); + }); +});