From 123e58474c9f3c8b0ff1e4dff9d15df934d7383f Mon Sep 17 00:00:00 2001 From: gru-bot Date: Thu, 26 Sep 2024 22:16:55 +0800 Subject: [PATCH] test: add unit test for src/babel/utils.ts --- src/babel/utils.gru.test.ts | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/babel/utils.gru.test.ts diff --git a/src/babel/utils.gru.test.ts b/src/babel/utils.gru.test.ts new file mode 100644 index 0000000000..2c3df57756 --- /dev/null +++ b/src/babel/utils.gru.test.ts @@ -0,0 +1,54 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/no-unused-vars */ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import * as babel from '@babel/core' +import { beforeEach, describe, expect, it } from 'vitest' +import type { PluginOptions } from './utils.ts' +import { isAtom } from './utils.ts' + +describe('isAtom', () => { + let t: typeof babel.types + + beforeEach(() => { + t = babel.types + }) + + it('should return true for core atom functions', () => { + const callee = t.identifier('atom') + expect(isAtom(t, callee)).toBe(true) + }) + + it('should return true for custom atom functions', () => { + const callee = t.identifier('customAtom') + const options: PluginOptions = { customAtomNames: ['customAtom'] } + expect(isAtom(t, callee, options.customAtomNames)).toBe(true) + }) + + it('should return false for non-atom functions', () => { + const callee = t.identifier('nonAtom') + expect(isAtom(t, callee)).toBe(false) + }) + + it('should return true for member expression with core atom function', () => { + const callee = t.memberExpression(t.identifier('obj'), t.identifier('atom')) + expect(isAtom(t, callee)).toBe(true) + }) + + it('should return true for member expression with custom atom function', () => { + const callee = t.memberExpression( + t.identifier('obj'), + t.identifier('customAtom'), + ) + const options: PluginOptions = { customAtomNames: ['customAtom'] } + expect(isAtom(t, callee, options.customAtomNames)).toBe(true) + }) + + it('should return false for member expression with non-atom function', () => { + const callee = t.memberExpression( + t.identifier('obj'), + t.identifier('nonAtom'), + ) + expect(isAtom(t, callee)).toBe(false) + }) +})