Skip to content

Commit

Permalink
feat(ics): allow providing .ics filename, without relying on title
Browse files Browse the repository at this point in the history
  • Loading branch information
prystupa authored and jshor committed Jan 5, 2021
1 parent d88773f commit ad356ed
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/ICalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ export default class ICalendar extends CalendarBase {
* Downloads the rendered iCalendar.
*
* @remark Only works in browsers.
*
* @param {string} filename optional explicit filename, if not provided then will be constructed from title
*/
public download = (): void => {
ics.download(this.title, this.render())
public download = (filename?: string): void => {
ics.download(filename || ics.getFileName(this.title), this.render())
}

/**
Expand Down
22 changes: 21 additions & 1 deletion src/__tests__/ICalendar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,27 @@ describe('ICalendar', () => {

expect(obj.render).toHaveBeenCalledTimes(1)
expect(ics.download).toHaveBeenCalledTimes(1)
expect(ics.download).toHaveBeenCalledWith(baseOpts.title, mockRender)
expect(ics.download).toHaveBeenCalledWith(`${baseOpts.title}.ics`, mockRender)
})
})

describe('download(filename)', () => {
it('should call render and the download util with provided filename', () => {
const obj = new ICalendar(baseOpts)
const mockRender = 'renderedstring'

jest
.spyOn(ics, 'download')
.mockImplementation(jest.fn())
jest
.spyOn(obj, 'render')
.mockReturnValue(mockRender)

obj.download('test.ics')

expect(obj.render).toHaveBeenCalledTimes(1)
expect(ics.download).toHaveBeenCalledTimes(1)
expect(ics.download).toHaveBeenCalledWith('test.ics', mockRender)
})
})

Expand Down
5 changes: 2 additions & 3 deletions src/utils/ics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,10 @@ const isSafari = (): boolean => {
/**
* Downloads the given ics as an iCalendar file.
*
* @param {string} title - title of the event
* @param {string} fileName - filename of the event file
* @param {string} data - ics data
*/
const download = (title: string, data: string): void => {
const fileName = getFileName(title)
const download = (fileName: string, data: string): void => {

if (isSafari()) {
safariFileSave(data, fileName)
Expand Down

0 comments on commit ad356ed

Please sign in to comment.