{"size": 1244, "ext": "js", "max_stars_repo_path": "__tests__/providers/webhook/webhook.js", "max_stars_repo_name": "saurabharch/notifme-sdk", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "__tests__/providers/webhook/webhook.js", "max_issues_repo_name": "saurabharch/notifme-sdk", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "__tests__/providers/webhook/webhook.js", "max_forks_repo_name": "saurabharch/notifme-sdk", "max_forks_repo_forks_event_min_datetime": "2020-04-20T11:56:58.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-08T16:27:28.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 21.8245614035, "max_line_length": 96, "alphanum_fraction": 0.5908360129}
/* @flow */
/* global jest, test, expect */
import NotifmeSdk from '../../../src'
import mockHttp, {mockResponse} from '../mockHttp'

jest.mock('../../../src/util/logger', () => ({
  warn: jest.fn()
}))

const sdk = new NotifmeSdk({
  channels: {
    webhook: {
      providers: [{
        type: 'webhook'
      }]
    }
  }
})

const request = {
  webhook: {
    event: 'custom-event',
    data: {key: 'value'},
    url: 'https://google.fr'
  }
}

test('Webhook success.', async () => {
  mockResponse(200, 'ok')
  const result = await sdk.send(request)
  expect(mockHttp).lastCalledWith(expect.objectContaining({
    method: 'POST',
    href: 'https://google.fr/'
  }))
  expect(mockHttp.body).toContain(JSON.stringify({event: 'custom-event', data: {key: 'value'}}))
  expect(result).toEqual({
    status: 'success',
    channels: {
      webhook: {id: '', providerId: 'webhook-provider'}
    }
  })
})

test('Webhook with no message.', async () => {
  mockResponse(500, 'custom_webhook_error')
  const result = await sdk.send(request)
  expect(result).toEqual({
    status: 'error',
    errors: {
      webhook: '500 - custom_webhook_error'
    },
    channels: {
      webhook: {id: undefined, providerId: 'webhook-provider'}
    }
  })
})
