Skip to content

Commit

Permalink
tests are now involved in all this
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Alexandre St-Jean committed Apr 26, 2016
1 parent 5d570a6 commit 6f7f356
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 111 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_STORE

dist/*
dist_test/*
node_modules/*
typings/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ![coveo.analytics](media/header.png)
# ![coveo.analytics](header.png)

[![Build Status](https://travis-ci.org/coveo/coveo.analytics.js.svg?branch=master)](https://travis-ci.org/coveo/coveo.analytics.js)
[![dependency status](https://david-dm.org/coveo/coveo.analytics.js.svg)](https://david-dm.org/coveo/coveo.analytics.js)
Expand Down
File renamed without changes
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build:webpack": "webpack",
"build:tsc": "tsc",
"test": "tsc -p test && ava",
"test": "tsc -p tsconfig.test.json && ava \"**/*test.js\"",
"clean": "rm -rf dist/*"
},
"author": "Pierre-Alexandre St-Jean <[email protected]>",
Expand All @@ -20,7 +20,10 @@
"ava": "^0.14.0",
"es6-promise": "3.1.2",
"exports-loader": "0.6.3",
"express": "^4.13.4",
"isomorphic-fetch": "2.2.1",
"jsdom": "^8.4.0",
"sinon": "^1.17.3",
"ts-loader": "0.8.2",
"tslint": "3.8.0",
"tslint-loader": "2.1.3",
Expand All @@ -35,5 +38,11 @@
"dist/**/*.js.map",
"src/**/*.ts",
"LICENSE"
]
],
"ava": {
"require": [
"babel-register",
"./test/helpers/setup-browser-env.js"
]
}
}
11 changes: 6 additions & 5 deletions src/SimpleAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import * as analytics from './analytics';
import objectAssign from './objectAssign';

// SimpleAPI mimics the GoogleAnalytics API.
class SimpleAPI {
export class SimpleAPI {
private client: analytics.Client;

// init initializes a new SimpleAPI client.
// @param token is your coveo access_token / api_key / ...
// @param endpoint is the endpoint you want to target defaults to the
// usage analytics production endpoint
init(token: string, endpoint: string = analytics.Endpoints.default ) {
init(token: string, endpoint: string ): void {
endpoint = endpoint || analytics.Endpoints.default;
if (typeof token === 'undefined') {
throw new Error(`You must pass your token when you call 'init'`);
}
Expand All @@ -20,7 +21,7 @@ class SimpleAPI {
});
}

send(event: EventType, customData: any) {
send(event: EventType, customData: any): void {
if (typeof this.client == 'undefined') {
throw new Error(`You must call init before sending an event`);
}
Expand Down Expand Up @@ -50,10 +51,10 @@ type EventType = 'pageview';
// simpleAPI singleton
const simpleAPI = new SimpleAPI();

export const SimpleAnalytics = (action: string, ...params: string[]) => {
export const SimpleAnalytics = (action: string, ...params: any[]): any => {
const actionFunction = (<any>simpleAPI)[action];
if (actionFunction) {
actionFunction.apply(simpleAPI, params);
return actionFunction.apply(simpleAPI, params);
}
};

Expand Down
9 changes: 8 additions & 1 deletion src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ export class Client {
private endpoint: string;
private token: string;

constructor(opts: ClientOptions = {token: ''} ) {
constructor(opts: ClientOptions) {
if (typeof opts === 'undefined') {
throw new Error('You have to pass options to this constructor');
}
if (typeof opts.token === 'undefined') {
throw new Error('You have to pass opts.token');
}

this.endpoint = opts.endpoint || Endpoints.default;
this.token = opts.token;
}
Expand Down
6 changes: 3 additions & 3 deletions src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import SimpleAnalytics from './SimpleAnalytics';
import SimpleAnalytics from './simpleanalytics';
import * as analytics from './index';
import { HistoryStore } from './history';

Expand All @@ -7,9 +7,9 @@ declare const global: any;
// CoveoUAGlobal is the interface for the global function which also has a
// queue `q` of unexecuted parameters
export interface CoveoUAGlobal {
(action: string, ...params: Array<string>): void;
(action: string, ...params: string[]): void;
// CoveoAnalytics.q is the queue of last called actions before lib was included
q?: Array<Array<string>>;
q?: string[][];
disableAutoHistory: boolean;
}

Expand Down
57 changes: 0 additions & 57 deletions src/history.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as analytics from './analytics';
import * as SimpleAnalytics from './SimpleAnalytics';
import * as SimpleAnalytics from './simpleanalytics';
import * as history from './history';

export {
Expand Down
33 changes: 33 additions & 0 deletions test/analytics_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import test from 'ava';
import * as analytics from '../src/analytics';

import * as express from 'express';
import * as http from 'http';

var app:express.Application = express();
const server: http.Server = (<any>http).createServer(app).listen()
app.set('port', server.address().port);

test('Analytics: can post a view event', t => {

const response : analytics.ViewEventResponse = {
raw:undefined,
visitId : '123',
visitorId: '213'
}

app.post('/analytics/view',(req,res) => {
res.status(200).send(JSON.stringify(response));
});

const client = new analytics.Client({
token: 'token',
endpoint:`http://localhost:${server.address().port}`
});

return client.sendViewEvent({location:'here'}).then((res: analytics.ViewEventResponse)=> {
t.not(res.raw,undefined);
t.is(res.visitId,response.visitId);
t.is(res.visitorId,response.visitorId);
});
});
4 changes: 4 additions & 0 deletions test/helpers/setup-browser-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global.document = require('jsdom').jsdom('<body></body>');
global.window = document.defaultView;
global.navigator = window.navigator;
global.fetch = require('isomorphic-fetch');
10 changes: 10 additions & 0 deletions test/lib.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
interface IteratorResult<T> {
done: boolean;
value?: T;
}

interface Iterator<T> {
next(value?: any): IteratorResult<T>;
return?(value?: any): IteratorResult<T>;
throw?(e?: any): IteratorResult<T>;
}
32 changes: 32 additions & 0 deletions test/simpleanalytics_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import test from 'ava';
import simpleanalytics from '../src/simpleanalytics';

import * as express from 'express';
import * as http from 'http';

var app:express.Application = express();
const responder = (req: express.Request,res: express.Response) => {
res.status(200).send("{}")
}
app.post('/analytics/view',responder);
const server: http.Server = (<any>http).createServer(app).listen()
app.set('port', server.address().port);


test('SimpleAnalytics: can\'t call without initiating', t => {
t.throws(()=>{ simpleanalytics('send') }, /init/)
});
test('SimpleAnalytics: can\'t init without token', t => {
t.throws(()=>{ simpleanalytics('init') }, /token/);
});

test('SimpleAnalytics: can send pageview', t => {
simpleanalytics('init','MYTOKEN', `http://localhost:${server.address().port}`)
simpleanalytics('send','pageview')
});


test('SimpleAnalytics: can send pageview with customdata', t => {
simpleanalytics('init','MYTOKEN', `http://localhost:${server.address().port}`)
simpleanalytics('send','pageview', {somedata:'asd'});
});
11 changes: 0 additions & 11 deletions test/test.ts

This file was deleted.

6 changes: 0 additions & 6 deletions test/tsconfig.json

This file was deleted.

39 changes: 20 additions & 19 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
{
"compilerOptions": {
"target": "es5",
"outDir": "dist",
"module": "commonjs",
"declaration": true,
"noImplicitAny": true,
"removeComments": true,
"sourceMap": true
},
"files": [
"src/browser.ts",
"src/analytics.ts",
"src/index.ts",
"src/objectAssign.ts",
"src/donottrack.ts",
"src/history.ts",
"src/detector.ts",
"typings/main.d.ts"
]
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"outDir": "dist",
"declaration": true,
"noImplicitAny": true,
"removeComments": true,
"sourceMap": true
},
"files": [
"src/analytics.ts",
"src/browser.ts",
"src/detector.ts",
"src/donottrack.ts",
"src/history.ts",
"src/index.ts",
"src/objectassign.ts",
"src/simpleanalytics.ts",
"typings/main.d.ts"
]
}
17 changes: 17 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"outDir": "dist_test",
"declaration": false,
"noImplicitAny": true,
"removeComments": true,
"sourceMap": true
},
"files": [
"test/lib.d.ts",
"test/simpleanalytics_test.ts",
"test/analytics_test.ts",
"typings/main.d.ts"
]
}
8 changes: 6 additions & 2 deletions typings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"ambientDependencies": {
"es6-promise": "github:DefinitelyTyped/DefinitelyTyped/es6-promise/es6-promise.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
"isomorphic-fetch": "github:DefinitelyTyped/DefinitelyTyped/isomorphic-fetch/isomorphic-fetch.d.ts#56295f5058cac7ae458540423c50ac2dcf9fc711"
"es6-promise": "registry:dt/es6-promise#0.0.0+20160423074304",
"express": "registry:dt/express#4.0.0+20160317120654",
"express-serve-static-core": "registry:dt/express-serve-static-core#0.0.0+20160322035842",
"isomorphic-fetch": "github:DefinitelyTyped/DefinitelyTyped/isomorphic-fetch/isomorphic-fetch.d.ts#56295f5058cac7ae458540423c50ac2dcf9fc711",
"serve-static": "registry:dt/serve-static#0.0.0+20160317120654",
"sinon": "registry:dt/sinon#1.16.0+20160317120654"
}
}
6 changes: 3 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ module.exports = {
},
plugins:[
new webpack.ProvidePlugin({
'Promise': 'exports?global.Promise!es6-promise',
'window.fetch': 'exports?self.fetch!isomorphic-fetch',
'Promise': 'es6-promise',
'fetch': 'exports?self.fetch!whatwg-fetch'
}),
new webpack.optimize.UglifyJsPlugin()
],
ts: {
compilerOptions: {
"declaration": false,
declaration: false,
}
},
tslint: {
Expand Down

0 comments on commit 6f7f356

Please sign in to comment.