Last active
October 12, 2018 01:26
-
-
Save AquiTCD/81294f568b823e5eecca11a182a69e14 to your computer and use it in GitHub Desktop.
gas-jest-sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('./setup.js') | |
const doPost = require('../src/doPost.js').default | |
describe('doPost', () => { | |
let contents | |
describe('when event comes from GitHub', () => { | |
beforeEach(() => { | |
contents = require('./fixtures/gh_pr.json') | |
}) | |
it('notify to cw to use props from spreadsheet', () => { | |
const e = { postData: { contents: JSON.stringify(contents) } } | |
expect(doPost(e)).toEqual({ | |
room_id: '012345', | |
body: '[To:111111][info][title]📝レビュー依頼(From: 偽羽生さん)[/title]Project: Hello-World\nPR-#1: Update the README with new information\nURL: https://github.com/Codertocat/Hello-World/pull/1[/info]', | |
}) | |
}) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// mocks for built-in functions of GAS | |
SpreadsheetApp['getActiveSpreadsheet'] = jest.fn(() => new SpreadSheet) | |
PropertiesService['getScriptProperties'] = jest.fn(() => new Properties) | |
Logger['log'] = jest.fn((val) => console.log(val)) | |
// mocks for libraries of GAS | |
Underscore['load'] = jest.fn(() => require('lodash')) | |
Moment['moment'] = jest.fn((args) => require('moment')(args)) | |
ChatWorkClient['factory'] = jest.fn((tokenObj) => new Client(tokenObj)) | |
// override cosole.log as mock | |
global.spyLog = jest.spyOn(console, 'log') | |
spyLog.mockImplementation(x => x) | |
class SpreadSheet { | |
constructor() { | |
this.sheet1 = new Sheet('repos') | |
this.sheet2 = new Sheet('members') | |
this.sheet3 = new Sheet('log') | |
} | |
// override | |
getSheetByName(name) { | |
if (name === 'repos') return this.sheet1 | |
if (name === 'members') return this.sheet2 | |
if (name === 'log') return this.sheet3 | |
} | |
} | |
class Sheet { | |
constructor(name) { | |
this.data = [] | |
if (name === 'log') { | |
this.data.push( | |
['TIMESTAMP', 'TYPE', 'TITLE', 'BODY', 'DATA'] | |
) | |
} else if (name === 'repos') { | |
this.data.push( | |
['Name', 'Repository URL', 'Room ID', 'Bot'], | |
['test-repo-gh', 'https://github.com/AquiTCD/webhook-test', '000000', 'MOBSOL'], | |
['Hello-World', 'https://github.com/Codertocat/Hello-World', '012345', 'SEESAA'], | |
) | |
} else if (name === 'members') { | |
this.data.push( | |
['Name', 'ChatWork ID', 'GitHub ID'], | |
['土田', '111111', 'AquiTCD'], | |
['偽羽生', '222222', 'Codertocat'], | |
) | |
} | |
} | |
getLastRow() { | |
return this.data.length | |
} | |
getRange(startRow, startCol, rangeRow, rangeCol = 1) { | |
const rows = this.data.slice(startRow - 1, startRow - 1 + rangeRow) | |
let rangedRows = [] | |
rows.forEach((row) => { | |
let rangedCols = row.slice(startCol - 1, startCol - 1 + rangeCol) | |
rangedRows.push(rangedCols) | |
}) | |
return new Range(rangedRows) | |
} | |
} | |
class Range { | |
constructor(arr) { | |
this.data = arr | |
} | |
getValue() { | |
return this.data[0][0] | |
} | |
getValues() { | |
return this.data | |
} | |
setValues(values) { | |
console.log(values) | |
} | |
} | |
class Properties { | |
constructor() { | |
this.properties = { | |
CW_API_TOKEN_DEV: 'devtoken0123456789', | |
CW_API_TOKEN_MOBSOL: 'mobsoltoken0123456789', | |
} | |
} | |
// override | |
getProperty(name) { | |
return this.properties[name] | |
} | |
} | |
class Client { | |
constructor(tokenObj) { | |
this.token = tokenObj.token | |
} | |
sendMessage(msgObj) { | |
return msgObj | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// mocks for built-in functions of GAS | |
SpreadsheetApp['getActiveSpreadsheet'] = jest.fn(() => new SpreadSheet) | |
PropertiesService['getScriptProperties'] = jest.fn(() => new Properties) | |
Logger['log'] = jest.fn((val) => console.log(val)) | |
// mocks for libraries of GAS | |
Underscore['load'] = jest.fn(() => require('lodash')) | |
Moment['moment'] = jest.fn((args) => require('moment')(args)) | |
ChatWorkClient['factory'] = jest.fn((tokenObj) => new Client(tokenObj)) | |
// override cosole.log as mock | |
global.spyLog = jest.spyOn(console, 'log') | |
spyLog.mockImplementation(x => x) | |
class SpreadSheet { | |
constructor() { | |
this.sheet1 = new Sheet('repos') | |
this.sheet2 = new Sheet('members') | |
this.sheet3 = new Sheet('log') | |
} | |
// override | |
getSheetByName(name) { | |
if (name === 'repos') return this.sheet1 | |
if (name === 'members') return this.sheet2 | |
if (name === 'log') return this.sheet3 | |
} | |
} | |
class Sheet { | |
constructor(name) { | |
this.data = [] | |
if (name === 'log') { | |
this.data.push( | |
['TIMESTAMP', 'TYPE', 'TITLE', 'BODY', 'DATA'] | |
) | |
} else if (name === 'repos') { | |
this.data.push( | |
['Name', 'Repository URL', 'Room ID', 'Bot'], | |
['test-repo-gh', 'https://github.com/AquiTCD/webhook-test', '000000', 'MOBSOL'], | |
['Hello-World', 'https://github.com/Codertocat/Hello-World', '012345', 'SEESAA'], | |
) | |
} else if (name === 'members') { | |
this.data.push( | |
['Name', 'ChatWork ID', 'GitHub ID'], | |
['土田', '111111', 'AquiTCD'], | |
['偽羽生', '222222', 'Codertocat'], | |
) | |
} | |
} | |
getLastRow() { | |
return this.data.length | |
} | |
getRange(startRow, startCol, rangeRow, rangeCol = 1) { | |
const rows = this.data.slice(startRow - 1, startRow - 1 + rangeRow) | |
let rangedRows = [] | |
rows.forEach((row) => { | |
let rangedCols = row.slice(startCol - 1, startCol - 1 + rangeCol) | |
rangedRows.push(rangedCols) | |
}) | |
return new Range(rangedRows) | |
} | |
} | |
class Range { | |
constructor(arr) { | |
this.data = arr | |
} | |
getValue() { | |
return this.data[0][0] | |
} | |
getValues() { | |
return this.data | |
} | |
setValues(values) { | |
console.log(values) | |
} | |
} | |
class Properties { | |
constructor() { | |
this.properties = { | |
CW_API_TOKEN_DEV: 'devtoken0123456789', | |
CW_API_TOKEN_MOBSOL: 'mobsoltoken0123456789', | |
} | |
} | |
// override | |
getProperty(name) { | |
return this.properties[name] | |
} | |
} | |
class Client { | |
constructor(tokenObj) { | |
this.token = tokenObj.token | |
} | |
sendMessage(msgObj) { | |
return msgObj | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment