Skip to content

Testing Suite #8

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

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
dff81f6
Add chai and mocha for testing
jamesandrewclarke May 14, 2018
1566a78
Update package lock
jamesandrewclarke May 14, 2018
ace0a50
Update package-lock again??
jamesandrewclarke May 14, 2018
ca196dc
Add initial Unit test for Players endpoint
jamesandrewclarke May 14, 2018
7a34d53
Update tests script
jamesandrewclarke May 14, 2018
bd89f79
Rename file to test.js for simplicity
jamesandrewclarke May 14, 2018
08fba1e
Finish Players test
jamesandrewclarke May 14, 2018
734711b
Add 401 response
jamesandrewclarke May 17, 2018
b6876e6
Add 404 response
jamesandrewclarke May 17, 2018
2014ddb
Fix responses index file and remove TODO comment because that's not a…
jamesandrewclarke May 17, 2018
eb33a30
Add Client unit test, plus a new function that rejects invalid shards
jamesandrewclarke May 23, 2018
fdd485d
Fix code to comply with eslint
ekralc May 23, 2018
5009c82
Put Client error messages in external constants file, to keep tests c…
ekralc May 23, 2018
440170d
Move default shard into constants file, should it ever need to be cha…
ekralc May 23, 2018
ccf48a8
Merge branch 'feature/default-shard' into develop
ekralc May 23, 2018
e2552cb
Merge branch 'develop' into feature/testsuite/main
ekralc May 23, 2018
2dee083
Implement default shard constant into tests
ekralc May 23, 2018
856fe65
Remove the random blank line
ekralc May 23, 2018
e1608ff
Add invalid API key logic
ekralc May 24, 2018
44e0bf5
Merge branch 'feature/testsuite/main' of github.com:Boserr/pubg.js in…
ekralc May 24, 2018
5068ac5
Change api key in test scripts to be consistent
ekralc May 24, 2018
cd251b8
Change error message for shard IDs
ekralc May 24, 2018
1aae256
Merge branch 'master' into feature/testsuite/main
ekralc May 24, 2018
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
125 changes: 122 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"test": "npm run lint && npm run tests",
"lint": "eslint src *.js",
"tests": "node test/index.js",
"tests": "mocha \"test/**/*.js\"",
"webpack": "./node_modules/.bin/webpack --config webpack.config.js",
"docs": "./node_modules/.bin/jsdoc src src/matches src/playerseason README.md -t ./node_modules/minami && echo pubg.js.org > ./out/CNAME"
},
Expand All @@ -30,9 +30,11 @@
"snekfetch": "^4.0.0"
},
"devDependencies": {
"chai": "^4.1.2",
"eslint": "^4.19.1",
"jsdoc": "^3.5.5",
"minami": "^1.2.3",
"mocha": "^5.1.1",
"uglifyjs-webpack-plugin": "^1.2.5",
"webpack": "^3.11.0"
}
Expand Down
10 changes: 8 additions & 2 deletions src/Client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const snekfetch = require('snekfetch');
const Package = require('../package.json');

const Constants = require('./util/Constants');
const Util = require('./util/Util');
const Errors = require('./util/Errors');
const Player = require('./Player');
const Match = require('./matches/Match');
const Status = require('./Status');
Expand All @@ -14,9 +16,13 @@ const PlayerSeason = require('./playerseason/PlayerSeason');
* @param {string} [defaultShard='pc-oc'] Default shard to use if none provided in methods
*/
class Client {
constructor(key, defaultShard = 'pc-oc') {
constructor(key, defaultShard = Constants.DEFAULT_SHARD) {
if (!key) {
throw new Error('No API key passed.');
throw new Error(Errors.NO_API_KEY);
} else if (typeof key !== 'string') {
throw new Error(Errors.INVALID_API_KEY);
} else if (!Util.verifyShard(defaultShard)) {
throw new Error(Errors.NON_EXISTENT_SHARD_ID);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = {
BASE_URL: 'https://api.playbattlegrounds.com',

DEFAULT_SHARD: 'pc-oc',

SHARDS: [
'xbox-as',
'xbox-eu',
Expand Down
8 changes: 8 additions & 0 deletions src/util/Errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
NO_API_KEY: 'No API key provided',

INVALID_SHARD_ID: 'Invalid shard/region ID provided',
INVALID_API_KEY: 'Invalid API key provided',

NON_EXISTENT_SHARD_ID: 'Shard/region ID provided does not exist',
};
40 changes: 40 additions & 0 deletions test/Client/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const expect = require('chai').expect

const pubg = require('../../')
const Errors = require('../../src/util/Errors')
const Constants = require('../../src/util/Constants')

const Client = pubg.Client

describe('Client', function() {
it('should throw an error when an API key is not passed to it', function() {
expect(function() {
client = new Client()
}).to.throw(Error).with.property('message', Errors.NO_API_KEY)
})

it('should throw an error when a non-string is passed as an API key', function() {
expect(function() {
client = new Client(123)
}).to.throw(Error).with.property('message', Errors.INVALID_API_KEY)
})

it('should throw an error if an invalid region is passed to it', function() {
expect(function() {
client = new Client('non-existent-key', 'france')
}).to.throw(Error).with.property('message', Errors.NON_EXISTENT_SHARD_ID)
})

it('should store the API key and shard correctly', function() {
ApiKey = 'non-existent-key'
region = 'pc-na'
client = new Client(ApiKey, region)
expect(client).to.have.property('key').that.equals(ApiKey)
expect(client).to.have.property('defaultShard').that.equals(region)
})

it('should set a defaultShard if one is not specified', function() {
client = new Client('non-existent-key')
expect(client).to.have.property('defaultShard', Constants.DEFAULT_SHARD)
})
})
Loading