@mintlify/previewing
Version:
Preview Mintlify docs locally
143 lines (142 loc) • 5.55 kB
JavaScript
import { LOCAL_LINKED_CLI_VERSION } from '../constants.js';
import * as client from '../local-preview/client.js';
import { silentUpdateClient } from '../local-preview/update.js';
vi.mock('../local-preview/client.js', () => ({
getLatestClientVersion: vi.fn().mockResolvedValue('0.0.100'),
downloadTargetMint: vi.fn().mockResolvedValue(undefined),
tryDownloadTargetMint: vi.fn().mockResolvedValue(undefined),
getCompatibleClientVersion: vi.fn().mockResolvedValue('latest'),
}));
const tryDownloadTargetMintSpy = vi.spyOn(client, 'tryDownloadTargetMint');
describe('silentUpdateClient', () => {
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
vi.clearAllMocks();
});
it('has latest client version already', async () => {
const versionString = '0.0.100';
const clientVersion = undefined;
const cliVersion = undefined;
const { needsUpdate, error } = await silentUpdateClient({
versionString,
clientVersion,
cliVersion,
});
expect(tryDownloadTargetMintSpy).not.toHaveBeenCalled();
expect(needsUpdate).toBe(false);
expect(error).toBeUndefined();
});
it('has no client version and downloads latest', async () => {
const versionString = null;
const clientVersion = undefined;
const cliVersion = undefined;
const { needsUpdate, error } = await silentUpdateClient({
versionString,
clientVersion,
cliVersion,
});
expect(tryDownloadTargetMintSpy).toHaveBeenCalledWith({
targetVersion: '0.0.100',
existingVersion: null,
});
expect(needsUpdate).toBe(false);
expect(error).toBeUndefined();
});
it('always downloads latest for locally linked cli', async () => {
const versionString = null;
const clientVersion = undefined;
const cliVersion = LOCAL_LINKED_CLI_VERSION;
const { needsUpdate, error } = await silentUpdateClient({
versionString,
clientVersion,
cliVersion,
});
expect(tryDownloadTargetMintSpy).toHaveBeenCalledWith({
targetVersion: '0.0.100',
existingVersion: null,
});
expect(needsUpdate).toBe(false);
expect(error).toBeUndefined();
});
it('compatible client version is latest and downloads latest', async () => {
const versionString = null;
const clientVersion = undefined;
const cliVersion = '4.2.0';
const { needsUpdate, error } = await silentUpdateClient({
versionString,
clientVersion,
cliVersion,
});
expect(tryDownloadTargetMintSpy).toHaveBeenCalledWith({
targetVersion: '0.0.100',
existingVersion: null,
});
expect(needsUpdate).toBe(false);
expect(error).toBeUndefined();
});
it('compatible client version is max and already has max', async () => {
vi.mocked(client.getCompatibleClientVersion).mockResolvedValueOnce('0.0.99');
const versionString = '0.0.99';
const clientVersion = undefined;
const cliVersion = '4.2.0';
const { needsUpdate, error } = await silentUpdateClient({
versionString,
clientVersion,
cliVersion,
});
expect(tryDownloadTargetMintSpy).not.toHaveBeenCalled();
expect(needsUpdate).toBe(true);
expect(error).toBeUndefined();
});
it('compatible client version is max and downloads max', async () => {
vi.mocked(client.getCompatibleClientVersion).mockResolvedValueOnce('0.0.99');
const versionString = '0.0.98';
const clientVersion = undefined;
const cliVersion = '4.2.0';
const { needsUpdate, error } = await silentUpdateClient({
versionString,
clientVersion,
cliVersion,
});
expect(tryDownloadTargetMintSpy).toHaveBeenCalledWith({
targetVersion: '0.0.99',
existingVersion: '0.0.98',
});
expect(needsUpdate).toBe(true);
expect(error).toBeUndefined();
});
it('compatible client version not found', async () => {
vi.mocked(client.getCompatibleClientVersion).mockResolvedValueOnce(undefined);
const versionString = '0.0.98';
const clientVersion = undefined;
const cliVersion = '4.2.0';
const { needsUpdate, error } = await silentUpdateClient({
versionString,
clientVersion,
cliVersion,
});
expect(tryDownloadTargetMintSpy).not.toHaveBeenCalled();
expect(needsUpdate).toBe(true);
expect(error).toBeUndefined();
});
it('error with downloading target mint', async () => {
vi.mocked(client.downloadTargetMint).mockRejectedValueOnce(new Error('some error'));
vi.mocked(client.tryDownloadTargetMint).mockResolvedValueOnce('some error');
const versionString = null;
const clientVersion = undefined;
const cliVersion = '4.2.0';
const { needsUpdate, error } = await silentUpdateClient({
versionString,
clientVersion,
cliVersion,
});
expect(tryDownloadTargetMintSpy).toHaveBeenCalledWith({
targetVersion: '0.0.100',
existingVersion: null,
});
expect(needsUpdate).toBe(false);
expect(error).toBe('some error');
});
});