node-onesky-utils
Version:
Node.js utils for working with OneSky translation service. Original package from @brainly/onesky-utils
175 lines (135 loc) • 5.78 kB
JavaScript
;
const getFiles = rootRequire('lib/getFiles.js');
const privateFunctions = rootRequire('lib/privateFunctions.js');
describe('getFiles', function() {
let sandbox;
beforeEach(function() {
sandbox = sinon.createSandbox();
});
afterEach(function() {
sandbox.restore();
});
describe('getFiles function', function() {
it('should call getDevHash with the secret', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
const getDevHashStub = sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves([]);
const options = {
projectId: 123,
secret: 'test-secret',
apiKey: 'test-api-key'
};
await getFiles(options);
expect(getDevHashStub).to.have.been.calledOnceWith('test-secret');
});
it('should call makeRequest with correct parameters', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves([]);
const options = {
projectId: 123,
secret: 'test-secret',
apiKey: 'test-api-key'
};
await getFiles(options);
expect(makeRequestStub).to.have.been.calledOnce;
const requestCall = makeRequestStub.getCall(0);
const requestOptions = requestCall.args[0];
const errorMessage = requestCall.args[1];
expect(requestOptions.method).to.equal('GET');
expect(requestOptions.url).to.include('/1/projects/123/files?');
expect(requestOptions.url).to.include('api_key=test-api-key');
expect(requestOptions.url).to.include('timestamp=1234567890');
expect(requestOptions.url).to.include('dev_hash=test-hash');
expect(errorMessage).to.equal('Unable to fetch project files');
});
it('should return the result from makeRequest', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
const expectedFiles = [
{ file_name: 'file1.json', locale: 'en' },
{ file_name: 'file2.po', locale: 'fr' }
];
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
sandbox.stub(privateFunctions, 'makeRequest').resolves(expectedFiles);
const options = {
projectId: 123,
secret: 'test-secret',
apiKey: 'test-api-key'
};
const result = await getFiles(options);
expect(result).to.deep.equal(expectedFiles);
});
it('should construct the correct API URL', async function() {
const mockHash = { devHash: 'abc123', timestamp: 9876543210 };
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves([]);
const options = {
projectId: 456,
secret: 'my-secret',
apiKey: 'my-api-key'
};
await getFiles(options);
const requestUrl = makeRequestStub.getCall(0).args[0].url;
expect(requestUrl).to.include('https://platform.api.onesky.io/1/projects/456/files?');
expect(requestUrl).to.include('api_key=my-api-key');
expect(requestUrl).to.include('timestamp=9876543210');
expect(requestUrl).to.include('dev_hash=abc123');
});
it('should not include locale or source_file_name parameters', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves([]);
const options = {
projectId: 123,
secret: 'secret',
apiKey: 'api-key'
};
await getFiles(options);
const requestUrl = makeRequestStub.getCall(0).args[0].url;
expect(requestUrl).to.not.include('locale=');
expect(requestUrl).to.not.include('source_file_name=');
});
it('should propagate errors from makeRequest', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
const expectedError = new Error('API error');
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
sandbox.stub(privateFunctions, 'makeRequest').rejects(expectedError);
const options = {
projectId: 123,
secret: 'test-secret',
apiKey: 'test-api-key'
};
try {
await getFiles(options);
expect.fail('Should have thrown an error');
} catch (error) {
expect(error).to.equal(expectedError);
}
});
it('should modify the options object by adding hash', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
sandbox.stub(privateFunctions, 'makeRequest').resolves([]);
const options = {
projectId: 123,
secret: 'test-secret',
apiKey: 'test-api-key'
};
await getFiles(options);
expect(options.hash).to.deep.equal(mockHash);
});
it('should handle empty response', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
sandbox.stub(privateFunctions, 'makeRequest').resolves([]);
const options = {
projectId: 123,
secret: 'test-secret',
apiKey: 'test-api-key'
};
const result = await getFiles(options);
expect(result).to.be.an('array');
expect(result).to.have.length(0);
});
});
});