'use strict';

const os = require('os');
const config = require('../fixtures/simple-config/webpack.config');
const testServer = require('../helpers/test-server');
const port = require('../ports-map').bonjour;

describe('bonjour option', () => {
  let server;
  const mockPublish = jest.fn();
  const mockUnpublishAll = jest.fn();

  beforeAll(() => {
    jest.mock('bonjour', () => () => {
      return {
        publish: mockPublish,
        unpublishAll: mockUnpublishAll,
      };
    });
  });

  afterEach(() => {
    mockPublish.mockReset();
    mockUnpublishAll.mockReset();
  });

  describe('http', () => {
    beforeEach((done) => {
      server = testServer.start(
        config,
        {
          bonjour: true,
          port,
        },
        done
      );
    });

    afterEach((done) => {
      server.close(done);
    });

    it('should call bonjour with correct params', () => {
      expect(mockPublish).toHaveBeenCalledTimes(1);
      expect(mockPublish).toHaveBeenCalledWith({
        name: `Webpack Dev Server ${os.hostname()}:${port}`,
        port,
        type: 'http',
        subtypes: ['webpack'],
      });
      expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
    });
  });

  describe('https option', () => {
    beforeEach((done) => {
      server = testServer.start(
        config,
        {
          bonjour: true,
          port,
          https: true,
        },
        done
      );
    });

    afterEach((done) => {
      server.close(done);
    });

    it('bonjour should use https when passed in option', () => {
      expect(mockPublish).toHaveBeenCalledTimes(1);
      expect(mockPublish).toHaveBeenCalledWith({
        name: `Webpack Dev Server ${os.hostname()}:${port}`,
        port,
        type: 'https',
        subtypes: ['webpack'],
      });
      expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
    });
  });

  describe('bonjour object', () => {
    beforeEach((done) => {
      server = testServer.start(
        config,
        {
          bonjour: {
            type: 'https',
            protocol: 'udp',
          },
          port,
        },
        done
      );
    });

    afterEach((done) => {
      server.close(done);
    });

    it('applies bonjour options', () => {
      expect(mockPublish).toHaveBeenCalledTimes(1);
      expect(mockPublish).toHaveBeenCalledWith({
        name: `Webpack Dev Server ${os.hostname()}:${port}`,
        port,
        type: 'https',
        protocol: 'udp',
        subtypes: ['webpack'],
      });
      expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
    });
  });
});

@@ -2582,13 +2582,776 @@
 );%0A  %7D);
+%0A%0A  describe('bonjour object and https', () =%3E %7B%0A    beforeEach((done) =%3E %7B%0A      server = testServer.start(%0A        config,%0A        %7B%0A          bonjour: %7B%0A            type: 'http',%0A            protocol: 'udp',%0A          %7D,%0A          https: true,%0A          port,%0A        %7D,%0A        done%0A      );%0A    %7D);%0A%0A    afterEach((done) =%3E %7B%0A      server.close(done);%0A    %7D);%0A%0A    it('prefers bonjour options over devServer.https', () =%3E %7B%0A      expect(mockPublish).toHaveBeenCalledTimes(1);%0A      expect(mockPublish).toHaveBeenCalledWith(%7B%0A        name: %60Webpack Dev Server $%7Bos.hostname()%7D:$%7Bport%7D%60,%0A        port,%0A        type: 'http',%0A        protocol: 'udp',%0A        subtypes: %5B'webpack'%5D,%0A      %7D);%0A      expect(mockUnpublishAll).toHaveBeenCalledTimes(0);%0A    %7D);%0A  %7D);
 %0A%7D);%0A

add more case for bonjour option (#3208)