Skip to content

Commit 5c08df0

Browse files
committed
add tests
1 parent 0ccac33 commit 5c08df0

File tree

6 files changed

+2360
-44
lines changed

6 files changed

+2360
-44
lines changed

.eslintrc.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
module.exports = {
2-
'extends': 'standard'
2+
extends: [
3+
'standard',
4+
'plugin:jest/recommended'
5+
],
6+
plugins: [ 'jest' ]
37
}

jest.config.js

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
// For a detailed explanation regarding each configuration property, visit:
2+
// https://jestjs.io/docs/en/configuration.html
3+
4+
module.exports = {
5+
// All imported modules in your tests should be mocked automatically
6+
// automock: false,
7+
8+
// Stop running tests after `n` failures
9+
// bail: 0,
10+
11+
// Respect "browser" field in package.json when resolving modules
12+
// browser: false,
13+
14+
// The directory where Jest should store its cached dependency information
15+
// cacheDirectory: "/private/var/folders/b0/rz6wn4bn1kd4k0fvddhsp4c00000gq/T/jest_dz",
16+
17+
// Automatically clear mock calls and instances between every test
18+
clearMocks: true,
19+
20+
// Indicates whether the coverage information should be collected while executing the test
21+
// collectCoverage: false,
22+
23+
// An array of glob patterns indicating a set of files for which coverage information should be collected
24+
// collectCoverageFrom: null,
25+
26+
// The directory where Jest should output its coverage files
27+
coverageDirectory: 'coverage',
28+
29+
// An array of regexp pattern strings used to skip coverage collection
30+
// coveragePathIgnorePatterns: [
31+
// "/node_modules/"
32+
// ],
33+
34+
// A list of reporter names that Jest uses when writing coverage reports
35+
// coverageReporters: [
36+
// "json",
37+
// "text",
38+
// "lcov",
39+
// "clover"
40+
// ],
41+
42+
// An object that configures minimum threshold enforcement for coverage results
43+
// coverageThreshold: null,
44+
45+
// A path to a custom dependency extractor
46+
// dependencyExtractor: null,
47+
48+
// Make calling deprecated APIs throw helpful error messages
49+
// errorOnDeprecated: false,
50+
51+
// Force coverage collection from ignored files using an array of glob patterns
52+
// forceCoverageMatch: [],
53+
54+
// A path to a module which exports an async function that is triggered once before all test suites
55+
// globalSetup: null,
56+
57+
// A path to a module which exports an async function that is triggered once after all test suites
58+
// globalTeardown: null,
59+
60+
// A set of global variables that need to be available in all test environments
61+
// globals: {},
62+
63+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
64+
// maxWorkers: "50%",
65+
66+
// An array of directory names to be searched recursively up from the requiring module's location
67+
// moduleDirectories: [
68+
// "node_modules"
69+
// ],
70+
71+
// An array of file extensions your modules use
72+
// moduleFileExtensions: [
73+
// "js",
74+
// "json",
75+
// "jsx",
76+
// "ts",
77+
// "tsx",
78+
// "node"
79+
// ],
80+
81+
// A map from regular expressions to module names that allow to stub out resources with a single module
82+
// moduleNameMapper: {},
83+
84+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
85+
// modulePathIgnorePatterns: [],
86+
87+
// Activates notifications for test results
88+
// notify: false,
89+
90+
// An enum that specifies notification mode. Requires { notify: true }
91+
// notifyMode: "failure-change",
92+
93+
// A preset that is used as a base for Jest's configuration
94+
preset: 'jest-puppeteer',
95+
96+
// Run tests from one or more projects
97+
// projects: null,
98+
99+
// Use this configuration option to add custom reporters to Jest
100+
// reporters: undefined,
101+
102+
// Automatically reset mock state between every test
103+
// resetMocks: false,
104+
105+
// Reset the module registry before running each individual test
106+
// resetModules: false,
107+
108+
// A path to a custom resolver
109+
// resolver: null,
110+
111+
// Automatically restore mock state between every test
112+
// restoreMocks: false,
113+
114+
// The root directory that Jest should scan for tests and modules within
115+
// rootDir: null,
116+
117+
// A list of paths to directories that Jest should use to search for files in
118+
// roots: [
119+
// "<rootDir>"
120+
// ],
121+
122+
// Allows you to use a custom runner instead of Jest's default test runner
123+
// runner: "jest-runner",
124+
125+
// The paths to modules that run some code to configure or set up the testing environment before each test
126+
// setupFiles: [],
127+
128+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
129+
// setupFilesAfterEnv: [],
130+
131+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
132+
// snapshotSerializers: [],
133+
134+
// The test environment that will be used for testing
135+
// testEnvironment: "node",
136+
137+
// Options that will be passed to the testEnvironment
138+
// testEnvironmentOptions: {},
139+
140+
// Adds a location field to test results
141+
// testLocationInResults: false,
142+
143+
// The glob patterns Jest uses to detect test files
144+
testMatch: [
145+
'**/tests/*.test.js'
146+
]
147+
148+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
149+
// testPathIgnorePatterns: [
150+
// "/node_modules/"
151+
// ],
152+
153+
// The regexp pattern or array of patterns that Jest uses to detect test files
154+
// testRegex: [],
155+
156+
// This option allows the use of a custom results processor
157+
// testResultsProcessor: null,
158+
159+
// This option allows use of a custom test runner
160+
// testRunner: "jasmine2",
161+
162+
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
163+
// testURL: "http://localhost",
164+
165+
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
166+
// timers: "real",
167+
168+
// A map from regular expressions to paths to transformers
169+
// transform: null,
170+
171+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
172+
// transformIgnorePatterns: [
173+
// "/node_modules/"
174+
// ],
175+
176+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
177+
// unmockedModulePathPatterns: undefined,
178+
179+
// Indicates whether each individual test should be reported during the run
180+
// verbose: null,
181+
182+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
183+
// watchPathIgnorePatterns: [],
184+
185+
// Whether to use watchman for file crawling
186+
// watchman: true,
187+
}

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"cron": "node src/cron.js",
99
"dev": "cross-env NODE_ENV=development node src/punchMembers out",
1010
"lint": "eslint --ext .js --ignore-path .gitignore --ignore-pattern !.eslintrc.js --max-warnings 0",
11-
"lintfix": "eslint --ext .js --ignore-path .gitignore --ignore-pattern !.eslintrc.js . --fix"
11+
"lintall": "eslint --ext .js --ignore-path .gitignore --ignore-pattern !.eslintrc.js --max-warnings 0 .",
12+
"lintfix": "eslint --ext .js --ignore-path .gitignore --ignore-pattern !.eslintrc.js . --fix",
13+
"test": "jest --watch"
1214
},
1315
"husky": {
1416
"hooks": {
@@ -44,10 +46,13 @@
4446
"eslint": "^6.0.1",
4547
"eslint-config-standard": "^12.0.0",
4648
"eslint-plugin-import": "^2.18.0",
49+
"eslint-plugin-jest": "^23.0.4",
4750
"eslint-plugin-node": "^9.1.0",
4851
"eslint-plugin-promise": "^4.2.1",
4952
"eslint-plugin-standard": "^4.0.0",
5053
"husky": "^2.7.0",
54+
"jest": "^24.9.0",
55+
"jest-puppeteer": "^4.3.0",
5156
"lint-staged": "^8.2.1"
5257
}
5358
}

src/member.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const puppeteer = require('puppeteer')
2+
const { urls } = require('../config')
3+
4+
class Member {
5+
constructor ({ id = '', password = '' }) {
6+
const isIdInvalid = typeof id !== 'string' || id === ''
7+
const isPasswordInvalid = typeof password !== 'string' || password === ''
8+
if (isIdInvalid || isPasswordInvalid) {
9+
throw new Error(
10+
`Please provide required id/password, some of your id: "${id}" or password: "${password}" is invalide`
11+
)
12+
}
13+
14+
this.id = id
15+
this.password = password
16+
this._browser = null
17+
}
18+
19+
get browser () {
20+
if (this._browser === null || this._browser.constructor.name !== 'Browser') {
21+
throw new Error('Browser was not been launched yet')
22+
}
23+
return this._browser
24+
}
25+
set browser (browser) {
26+
this._browser = browser
27+
}
28+
29+
async lunchBrowser () {
30+
const browser = await puppeteer.launch()
31+
this.browser = browser
32+
}
33+
34+
async login () {
35+
await this.lunchBrowser()
36+
const pageLogin = await this.browser.newPage()
37+
await pageLogin.goto(urls.pageLogin, { timeout: 0 })
38+
// await pageLogin.type(selectors.login.user, id)
39+
// await pageLogin.type(selectors.login.password, password)
40+
// const loginBtn = await pageLogin.$(selectors.login.submit)
41+
// await loginBtn.click()
42+
}
43+
}
44+
45+
// const theMember = new Member({ id: 'test', password: 'test' })
46+
// theMember.login()
47+
// // theMember.punch('in')
48+
// // theMember.punch('out')
49+
50+
module.exports = Member

tests/index.test.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const Member = require('../src/member')
2+
3+
const testMember = {
4+
id: 'testId',
5+
password: 'testPassword'
6+
}
7+
const testMemberMissingId = {
8+
password: 'testPassword'
9+
}
10+
const testMemberMissingPassword = {
11+
id: 'testId'
12+
}
13+
const testMemberInvalidId = {
14+
id: {},
15+
password: 'testPassword'
16+
}
17+
const testMemberInvalidPassword = {
18+
id: 'testId',
19+
password: {}
20+
}
21+
22+
describe('constructor validation', () => {
23+
it('should be initialize with proper member info', () => {
24+
const member = new Member(testMember)
25+
expect(member.id).toBe('testId')
26+
expect(member.password).toBe('testPassword')
27+
})
28+
it('should throw error if id is missing', () => {
29+
expect(() => new Member(testMemberMissingId))
30+
.toThrow()
31+
})
32+
it('should throw error if password is missing', () => {
33+
expect(() => new Member(testMemberMissingPassword))
34+
.toThrow()
35+
})
36+
it('should throw error if id is not string', () => {
37+
expect(() => new Member(testMemberInvalidId))
38+
.toThrow()
39+
})
40+
it('should throw error if password is not string', () => {
41+
expect(() => new Member(testMemberInvalidPassword))
42+
.toThrow()
43+
})
44+
})
45+
46+
describe('browser handling', () => {
47+
it('should throw error when getting the browser if we not launch manually yet', () => {
48+
const member = new Member(testMember)
49+
expect(() => member.browser).toThrow()
50+
})
51+
it('should return "Browser" if we access browser launched before', async () => {
52+
const member = new Member(testMember)
53+
await member.lunchBrowser()
54+
expect(member.browser.constructor.name).toBe('Browser')
55+
})
56+
})

0 commit comments

Comments
 (0)