Skip to content

Commit

Permalink
feat: add timezone input field (#19);
Browse files Browse the repository at this point in the history
fix: when no utcOffset is passed, force it to be set to UTC+0;
test: rewrite test code;
  • Loading branch information
josStorer committed Mar 4, 2023
1 parent 9104299 commit be5ff5c
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 83 deletions.
49 changes: 28 additions & 21 deletions action.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
const core = require("@actions/core");
const moment = require('moment');
const core = require('@actions/core')
const moment = require('moment-timezone')

function action () {
function action() {
try {
const utcOffset = core.getInput('utcOffset', { required: false });
const format = core.getInput('format', { required: false });
const utcOffset = core.getInput('utcOffset', {required: false})
const format = core.getInput('format', {required: false})
const timezone = core.getInput('timezone', {required: false})

const time = moment().utcOffset(utcOffset);
let time = moment()
if (timezone)
time = time.tz(timezone)
else if (utcOffset)
time = time.utcOffset(utcOffset)
else
time = time.utcOffset('+00:00')

core.setOutput("time", time.toISOString());
core.setOutput("ISOTime", time.toISOString());
core.setOutput("readableTime", time.toString());
core.setOutput("formattedTime", time.format(format));
core.setOutput('time', time.toISOString())
core.setOutput('ISOTime', time.toISOString())
core.setOutput('readableTime', time.toString())
core.setOutput('formattedTime', time.format(format))

let [year, month, day, hour, minute, second, millisecond] = time.toArray();
month = String(Number(month) + 1);
core.setOutput("year", year)
core.setOutput("month", month)
core.setOutput("day", day)
core.setOutput("hour", hour)
core.setOutput("minute", minute)
core.setOutput("second", second)
core.setOutput("millisecond", millisecond)
let [year, month, day, hour, minute, second, millisecond] = time.toArray()
month = Number(month) + 1
core.setOutput('year', year)
core.setOutput('month', month)
core.setOutput('day', day)
core.setOutput('hour', hour)
core.setOutput('minute', minute)
core.setOutput('second', second)
core.setOutput('millisecond', millisecond)
} catch (error) {
core.setFailed(error.message);
core.setFailed(error.message)
}
}

module.exports = action;
module.exports = action
151 changes: 100 additions & 51 deletions action.test.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,108 @@
const mockMoment = {
utcOffset: jest.fn().mockReturnThis(),
format: jest.fn(),
toISOString: jest.fn(),
toString: jest.fn(),
toArray: jest.fn()
};
jest.doMock('moment', () => () => mockMoment);

const mockCore = {
getInput: jest.fn(),
setOutput: jest.fn(),
setFailed: jest.fn(),
};
jest.doMock('@actions/core', () => mockCore);
setFailed: jest.fn()
}
jest.doMock('@actions/core', () => mockCore)

jest.spyOn(Date, 'now').mockReturnValue(new Date('2020-07-01T00:30:15.000Z'))

const action = require('./action.js');
const action = require('./action.js')

describe("action", () => {
it("Should load", () => {
expect(action).not.toBeNull();
});
describe('action', () => {
it('Should load', () => {
expect(action).not.toBeNull()
})

it("Should run with basic functionality", () => {
mockMoment.toISOString.mockReturnValue('##');
mockMoment.toString.mockReturnValue('##');
mockMoment.format.mockReturnValue('##');
action();
expect(mockCore.setOutput).toHaveBeenCalledWith('time', '##');
expect(mockCore.setOutput).toHaveBeenCalledWith('ISOTime', '##');
expect(mockCore.setOutput).toHaveBeenCalledWith('readableTime', '##');
expect(mockCore.setOutput).toHaveBeenCalledWith('formattedTime', '##');
});
it('Should correctly set outputs', () => {
jest.clearAllMocks()
mockCore.getInput = jest.fn().mockImplementation((input) => {
switch (input) {
case 'utcOffset':
return ''
case 'format':
return ''
case 'timezone':
return ''
}
})
action()
expect(mockCore.setOutput.mock.calls).toEqual([
['time', '2020-07-01T00:30:15.000Z'],
['ISOTime', '2020-07-01T00:30:15.000Z'],
['readableTime', 'Wed Jul 01 2020 00:30:15 GMT+0000'],
['formattedTime', '2020-07-01T00:30:15Z'],
['year', 2020],
['month', 7],
['day', 1],
['hour', 0],
['minute', 30],
['second', 15],
['millisecond', 0]
])
})

it("Should run with other basic outputs", () => {
mockMoment.toArray.mockReturnValue(['2020', '0', '1', '12', '0', '1', '2']);
action();
expect(mockCore.setOutput).toHaveBeenCalledWith('year', '2020');
expect(mockCore.setOutput).toHaveBeenCalledWith('month', '1');
expect(mockCore.setOutput).toHaveBeenCalledWith('day', '1');
expect(mockCore.setOutput).toHaveBeenCalledWith('hour', '12');
expect(mockCore.setOutput).toHaveBeenCalledWith('minute', '0');
expect(mockCore.setOutput).toHaveBeenCalledWith('second', '1');
expect(mockCore.setOutput).toHaveBeenCalledWith('millisecond', '2');
});
it('Should correctly set outputs with utcOffset', () => {
jest.clearAllMocks()
mockCore.getInput = jest.fn().mockImplementation((input) => {
switch (input) {
case 'utcOffset':
return '+08:00'
case 'format':
return 'YYYYMMDD-HH'
case 'timezone':
return ''
}
})
action()
expect(mockCore.setOutput.mock.calls).toEqual([
['time', '2020-07-01T00:30:15.000Z'],
['ISOTime', '2020-07-01T00:30:15.000Z'],
['readableTime', 'Wed Jul 01 2020 08:30:15 GMT+0800'],
['formattedTime', '20200701-08'],
['year', 2020],
['month', 7],
['day', 1],
['hour', 8],
['minute', 30],
['second', 15],
['millisecond', 0]
])
})

it("Should pass format inputs", () => {
mockCore.getInput.mockReturnValue('###');
action();
expect(mockMoment.utcOffset).toHaveBeenCalledWith('###');
expect(mockMoment.format).toHaveBeenCalledWith('###');
});
it('Should correctly set outputs with timezone', () => {
jest.clearAllMocks()
mockCore.getInput = jest.fn().mockImplementation((input) => {
switch (input) {
case 'utcOffset':
return '+08:00'
case 'format':
return 'YYYYMMDD-HH'
case 'timezone':
return 'America/Los_Angeles'
}
})
action()
expect(mockCore.setOutput.mock.calls).toEqual([
['time', '2020-07-01T00:30:15.000Z'],
['ISOTime', '2020-07-01T00:30:15.000Z'],
['readableTime', 'Tue Jun 30 2020 17:30:15 GMT-0700'],
['formattedTime', '20200630-17'],
['year', 2020],
['month', 6],
['day', 30],
['hour', 17],
['minute', 30],
['second', 15],
['millisecond', 0]
])
})

it("Should throw error", () => {
mockMoment.utcOffset = () => { throw new Error('####') }
action();
expect(mockCore.setFailed).toHaveBeenCalledWith('####');
});
});
it('Should throw error', () => {
jest.clearAllMocks()
mockCore.setOutput = () => {
throw new Error('#')
}
action()
expect(mockCore.setFailed).toHaveBeenCalledWith('#')
})
})
12 changes: 9 additions & 3 deletions dist/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const action = require('./action.js');
action();
const action = require('./action.js')
action()
23 changes: 20 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: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{
"name": "get-current-time",
"version": "2.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"moment": "^2.29.4"
"moment-timezone": "^0.5.41"
},
"devDependencies": {
"@types/jest": "^29.4.0",
Expand Down

0 comments on commit be5ff5c

Please sign in to comment.