{"size": 2464, "ext": "js", "max_stars_repo_path": "test/utils/extract-from-file.test.js", "max_stars_repo_name": "ttypic/react-intl-cli", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/utils/extract-from-file.test.js", "max_issues_repo_name": "ttypic/react-intl-cli", "max_issues_repo_issues_event_min_datetime": "2020-07-20T15:56:46.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-26T16:26:26.000Z", "max_forks_repo_path": "test/utils/extract-from-file.test.js", "max_forks_repo_name": "ttypic/react-intl-cli", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 7, "max_forks_count": null, "avg_line_length": 29.3333333333, "max_line_length": 115, "alphanum_fraction": 0.5401785714}
const extractFromFile = require('../../src/utils/extract-from-file');

const fs = require('fs');
const readFileSpy = jest.spyOn(fs, 'readFile');

beforeEach(() => {
    jest.clearAllMocks();
    process.env.NODE_ENV = 'development';
});

test('it provide empty output on file without messages', () => {
    readFileSpy.mockImplementation((filename, encoding, callback) => callback(undefined, ''));

    return extractFromFile('some-file.js').then(messages => expect(messages).toStrictEqual([]));
});

test('it extract messages', () => {
    const fileWithMessagesContent = `
        import React, { Component } from 'react';
        import { defineMessages } from 'react-intl';
        
        export const msgs = defineMessages({
          header: {
            id: 'foo.bar',
            defaultMessage: 'Hello World!'
          },
          content: {
            id: 'foo.foo',
            defaultMessage: 'Hi World!',
            description: 'Another message'
          }
        });
    `;

    readFileSpy.mockImplementation((filename, encoding, callback) => callback(undefined, fileWithMessagesContent));

    return extractFromFile('some-file.js').then(messages =>
        expect(messages).toEqual([
            {
                id: 'foo.bar',
                defaultMessage: 'Hello World!'
            },
            {
                id: 'foo.foo',
                defaultMessage: 'Hi World!',
                description: 'Another message'
            }
        ])
    );
});

test('it extract messages with macros', () => {
    const fileWithMessagesContent = `
        import React, { Component } from 'react';
        import defineMessages from 'define-messages.macro';
        
        defineMessages.setupPrefix('foo');
        
        export const msgs = defineMessages({
          header: 'Hello World!',
          content: {
            defaultMessage: 'Hi World!',
            description: 'Another message'
          }
        });
    `;

    readFileSpy.mockImplementation((filename, encoding, callback) => callback(undefined, fileWithMessagesContent));

    return extractFromFile('some-file.js').then(messages =>
        expect(messages).toEqual([
            {
                id: 'foo.header',
                defaultMessage: 'Hello World!'
            },
            {
                id: 'foo.content',
                defaultMessage: 'Hi World!',
                description: 'Another message'
            }
        ])
    );
});
