Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscarrollsmith committed Sep 25, 2023
1 parent 671de06 commit 90a0647
Show file tree
Hide file tree
Showing 12 changed files with 15,114 additions and 963 deletions.
131 changes: 119 additions & 12 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,125 @@
/**
* Unit tests for the action's entrypoint, src/index.ts
*/
// index.test.js
const core = require('@actions/core')
const fetch = require('isomorphic-fetch')
const fs = require('fs')
const { parseStringPromise } = require('xml2js')
const { fetchRssFeed } = require('../src/index')

const { run } = require('../src/main')

// Mock the action's entrypoint
jest.mock('../src/main', () => ({
run: jest.fn()
jest.mock('isomorphic-fetch')
jest.mock('fs', () => ({
accessSync: jest.fn(),
writeFileSync: jest.fn(),
constants: { W_OK: 'some_value' },
promises: {
access: jest.fn()
}
}))

describe('index', () => {
it('calls run when imported', async () => {
require('../src/index')
jest.mock('xml2js')
jest.mock('@actions/core')

describe('fetchRssFeed', () => {
beforeEach(() => {
jest.resetAllMocks()
})

it('should set failure if no feed URL is provided', async () => {
process.env.INPUT_FEED_URL = ''
await fetchRssFeed()
expect(core.setFailed).toHaveBeenCalledWith('Feed URL is not provided')
})

it('should set failure if fetch fails', async () => {
expect.assertions(1)
process.env.INPUT_FEED_URL = 'http://example.com/feed'
fetch.mockRejectedValue(new Error('Fetch failed'))

await fetchRssFeed()

expect(core.setFailed).toHaveBeenCalledWith('Fetch failed')
})

it('should save the feed to a file if file path is provided', async () => {
process.env.INPUT_FEED_URL = 'http://example.com/feed'
process.env.INPUT_FILE_PATH = './feed.json'
fetch.mockResolvedValue({
ok: true,
text: jest.fn().mockResolvedValue('<rss></rss>')
})
parseStringPromise.mockResolvedValue({ rss: {} })
fs.accessSync.mockReturnValue(true)

await fetchRssFeed()

expect(fs.writeFileSync).toHaveBeenCalledWith(
'./feed.json',
JSON.stringify({ rss: {} }, null, 2)
)
expect(core.setOutput).toHaveBeenCalledWith(
'feed',
JSON.stringify({ rss: {} }, null, 2)
)
})

it('should not save the feed to a file if no file path is provided', async () => {
process.env.INPUT_FEED_URL = 'http://example.com/feed'
process.env.INPUT_FILE_PATH = ''
fetch.mockResolvedValue({
ok: true,
text: jest.fn().mockResolvedValue('<rss></rss>')
})
parseStringPromise.mockResolvedValue({ rss: {} })

await fetchRssFeed()

expect(fs.writeFileSync).not.toHaveBeenCalled()
expect(core.setOutput).toHaveBeenCalledWith(
'feed',
JSON.stringify({ rss: {} }, null, 2)
)
})
})

// Verify that mocks work correctly
describe('fetch mock', () => {
it('should reject when mockRejectedValue is used', async () => {
// Set up the mock to reject
fetch.mockRejectedValue(new Error('Fetch failed'))

// Expect that calling fetch will reject with the specified error
await expect(fetch('some_url')).rejects.toThrow('Fetch failed')
})
})

describe('fs mock', () => {
it('should call accessSync without errors', () => {
fs.accessSync.mockReturnValue(true)
expect(() => fs.accessSync('some_path', fs.constants.W_OK)).not.toThrow()
})

it('should call writeFileSync without errors', () => {
fs.writeFileSync.mockReturnValue(true)
expect(() => fs.writeFileSync('some_path', 'some_data')).not.toThrow()
})
})

describe('xml2js mock', () => {
it('should resolve parseStringPromise', async () => {
parseStringPromise.mockResolvedValue({ rss: {} })
await expect(parseStringPromise('<rss></rss>')).resolves.toEqual({
rss: {}
})
})
})

describe('@actions/core mock', () => {
it('should call setOutput without errors', () => {
core.setOutput.mockReturnValue(true)
expect(() => core.setOutput('key', 'value')).not.toThrow()
})

expect(run).toHaveBeenCalled()
it('should call setFailed without errors', () => {
core.setFailed.mockReturnValue(true)
expect(() => core.setFailed('Some error')).not.toThrow()
})
})
100 changes: 0 additions & 100 deletions __tests__/main.test.js

This file was deleted.

24 changes: 0 additions & 24 deletions __tests__/wait.test.js

This file was deleted.

20 changes: 10 additions & 10 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
name: 'The name of your action here'
description: 'Provide a description here'
author: 'Your name or organization here'
name: 'RSS Feed Fetch Action'
description: 'Fetches an RSS feed and optionally saves it to a file'
author: 'Christopher C. Smith <[email protected]>'

# Define your inputs here.
inputs:
milliseconds:
description: 'Your input description here'
feed_url:
description: 'The URL of the RSS feed to fetch'
required: true
default: '1000'
file_path:
description: 'The relative file path to save the fetched RSS feed'
required: false

# Define your outputs here.
outputs:
time:
description: 'Your output description here'
feed:
description: 'The fetched RSS feed in JSON format'

runs:
using: node20
Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 90a0647

Please sign in to comment.