{"size": 2598, "ext": "js", "max_stars_repo_path": "lib/plugins/aws/package/compile/events/websockets/lib/validate.test.js", "max_stars_repo_name": "0xkalle/serverless", "max_stars_repo_stars_event_min_datetime": "2021-12-12T04:23:33.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-12T04:23:33.000Z", "max_issues_repo_path": "lib/plugins/aws/package/compile/events/websockets/lib/validate.test.js", "max_issues_repo_name": "errietta/serverless", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/plugins/aws/package/compile/events/websockets/lib/validate.test.js", "max_forks_repo_name": "errietta/serverless", "max_forks_repo_forks_event_min_datetime": "2019-02-24T10:59:38.000Z", "max_forks_repo_forks_event_max_datetime": "2019-02-24T10:59:38.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 25.2233009709, "max_line_length": 85, "alphanum_fraction": 0.5446497306}
'use strict';

const expect = require('chai').expect;
const AwsCompileWebsocketsEvents = require('../index');
const Serverless = require('../../../../../../../Serverless');
const AwsProvider = require('../../../../../provider/awsProvider');

describe('#validate()', () => {
  let serverless;
  let awsCompileWebsocketsEvents;

  beforeEach(() => {
    const options = {
      stage: 'dev',
      region: 'us-east-1',
    };
    serverless = new Serverless();
    serverless.setProvider('aws', new AwsProvider(serverless, options));
    awsCompileWebsocketsEvents = new AwsCompileWebsocketsEvents(serverless, options);
  });

  it('should support the simplified string syntax', () => {
    awsCompileWebsocketsEvents.serverless.service.functions = {
      first: {
        events: [
          {
            websocket: '$connect',
          },
        ],
      },
    };
    const validated = awsCompileWebsocketsEvents.validate();
    expect(validated.events).to.deep.equal([
      {
        functionName: 'first',
        route: '$connect',
      },
    ]);
  });

  it('should support the extended object syntax', () => {
    awsCompileWebsocketsEvents.serverless.service.functions = {
      first: {
        events: [
          {
            websocket: {
              route: '$connect',
            },
          },
        ],
      },
    };
    const validated = awsCompileWebsocketsEvents.validate();
    expect(validated.events).to.deep.equal([
      {
        functionName: 'first',
        route: '$connect',
      },
    ]);
  });

  it('should ignore non-websocket events', () => {
    awsCompileWebsocketsEvents.serverless.service.functions = {
      first: {
        events: [
          {
            ignored: {},
          },
        ],
      },
    };
    const validated = awsCompileWebsocketsEvents.validate();
    expect(validated.events).to.be.an('Array').with.length(0);
  });

  it('should reject a websocket event without a route', () => {
    awsCompileWebsocketsEvents.serverless.service.functions = {
      first: {
        events: [
          {
            websocket: {},
          },
        ],
      },
    };
    expect(() => awsCompileWebsocketsEvents.validate()).to.throw(/set the "route"/);
  });

  it('should reject a usage of both, http and websocket event types', () => {
    awsCompileWebsocketsEvents.serverless.service.functions = {
      first: {
        events: [
          {
            websocket: {},
            http: {},
          },
        ],
      },
    };
    expect(() => awsCompileWebsocketsEvents.validate()).to.throw(/can either be/);
  });
});
