Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow PercyAgent to be exported onto the window as PercyAgent #5

Merged
merged 4 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"repository": "percy/percy-agent",
"scripts": {
"build": "npm run clean && rm -rf dist && tsc && npm run build-client",
"build-client": "mkdir -p dist/public && browserify src/percy-agent-client/*.ts -p [ tsify --noImplicitAny ] --standalone Percy > dist/public/percy-agent.js",
"build-client": "mkdir -p dist/public && browserify src/percy-agent-client/index.ts -p [ tsify --noImplicitAny ] --standalone PercyAgent > dist/public/percy-agent.js",
"build-client-test": "npm run build-client && browserify test/percy-agent-client/*.ts -p [ tsify --noImplicitAny ] > dist-test/browserified-tests.js",
"clean": "rm -f .oclif.manifest.json",
"lint": "tsc -p test --noEmit && tslint -p test -t stylish --fix",
Expand Down
1 change: 1 addition & 0 deletions src/percy-agent-client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./percy-agent').default
13 changes: 13 additions & 0 deletions src/percy-agent-client/percy-agent-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class PercyAgentService {
xhr: XMLHttpRequest

constructor(xhr?: any) {
this.xhr = new xhr() || new XMLHttpRequest()
}

post(url: string, data: any) {
this.xhr.open('post', url, false) // synchronous request
this.xhr.setRequestHeader('Content-Type', 'application/json')
this.xhr.send(JSON.stringify(data))
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
class PercyAgent {
xhr: XMLHttpRequest
import {PercyAgentService} from './percy-agent-service'
import {SnapshotOptions} from './snapshot-options'

constructor(xhr?: any) {
this.xhr = new xhr() || new XMLHttpRequest()
}

post(url: string, data: any) {
this.xhr.open('post', url, false) // synchronous request
this.xhr.setRequestHeader('Content-Type', 'application/json')
this.xhr.send(JSON.stringify(data))
}
}

export interface SnapshotOptions {
enableJavascript?: boolean,
widths?: number[],
minimumHeight?: number,
document?: Document,
}

export class PercyAgentClient {
export default class PercyAgent {
userAgent: string | null
xhr: any
port: number
Expand All @@ -37,7 +19,7 @@ export class PercyAgentClient {
snapshot(name: string, options: SnapshotOptions = {}) {
let documentObject = options.document || document
let domSnapshot = this.domSnapshot(documentObject)
let percyAgent = new PercyAgent(this.xhr)
let percyAgent = new PercyAgentService(this.xhr)

percyAgent.post(`http://localhost:${this.port}/percy/snapshot`, {
name,
Expand Down
6 changes: 6 additions & 0 deletions src/percy-agent-client/snapshot-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface SnapshotOptions {
enableJavascript?: boolean,
widths?: number[],
minimumHeight?: number,
document?: Document,
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {expect} from 'chai'
import {PercyAgentClient} from '../../src/percy-agent-client/percy-agent-client'
require('../../src/percy-agent-client/percy-agent-client')
import PercyAgent from '../../src/percy-agent-client/percy-agent'
require('../../src/percy-agent-client/percy-agent')
import * as sinon from 'sinon'

describe('PercyAgentClient', () => {
describe('PercyAgent', () => {
let xhr = sinon.useFakeXMLHttpRequest()
const subject = new PercyAgentClient('Test Client', xhr)
const subject = new PercyAgent('Test Client', xhr)
let requests: sinon.SinonFakeXMLHttpRequest[] = []

beforeEach(() => {
Expand Down