Skip to content

Commit

Permalink
Repair the tests for @solana/compat since adding `fromLegacyTransac…
Browse files Browse the repository at this point in the history
…tionInstruction()` (#46)

Annoyingly, `Buffer` is not a symbol JS DOM or whatever so this test fails.
  • Loading branch information
steveluscher authored Jan 3, 2025
1 parent 5253f82 commit 8111c88
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
24 changes: 17 additions & 7 deletions packages/compat/src/__tests__/instruction-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import { PublicKey, TransactionInstruction } from '@solana/web3.js';
import { fromLegacyPublicKey } from '../address';
import { fromLegacyTransactionInstruction } from '../instruction';

function toLegacyByteArrayAppropriateForPlatform<TArrayBuffer extends ArrayBufferLike>(
data: WithImplicitCoercion<TArrayBuffer>,
): Buffer<TArrayBuffer> {
if (__NODEJS__) {
return Buffer.from(data);
} else {
return new Uint8Array(data as TArrayBuffer) as Buffer<TArrayBuffer>;
}
}

describe('fromLegacyTransactionInstruction', () => {
it('converts a basic TransactionInstruction', () => {
const programId = new Uint8Array([1, 2, 3, 4]);
Expand All @@ -20,7 +30,7 @@ describe('fromLegacyTransactionInstruction', () => {
const data = new Uint8Array([10, 20, 30]);

const instruction = new TransactionInstruction({
data: Buffer.from(data),
data: toLegacyByteArrayAppropriateForPlatform(data),
keys,
programId: new PublicKey(programId),
});
Expand Down Expand Up @@ -51,7 +61,7 @@ describe('fromLegacyTransactionInstruction', () => {
const data = new Uint8Array([10, 20, 30]);

const instruction = new TransactionInstruction({
data: Buffer.from(data),
data: toLegacyByteArrayAppropriateForPlatform(data),
keys,
programId: new PublicKey(programId),
});
Expand All @@ -73,7 +83,7 @@ describe('fromLegacyTransactionInstruction', () => {
const data = new Uint8Array([10, 20, 30]);

const instruction = new TransactionInstruction({
data: Buffer.from(data),
data: toLegacyByteArrayAppropriateForPlatform(data),
keys,
programId: new PublicKey(programId),
});
Expand All @@ -95,7 +105,7 @@ describe('fromLegacyTransactionInstruction', () => {
const data = new Uint8Array([10, 20, 30]);

const instruction = new TransactionInstruction({
data: Buffer.from(data),
data: toLegacyByteArrayAppropriateForPlatform(data),
keys,
programId: new PublicKey(programId),
});
Expand All @@ -110,7 +120,7 @@ describe('fromLegacyTransactionInstruction', () => {
const data = new Uint8Array([40, 50, 60]);

const instruction = new TransactionInstruction({
data: Buffer.from(data),
data: toLegacyByteArrayAppropriateForPlatform(data),
keys: [],
programId: new PublicKey(programId),
});
Expand All @@ -136,7 +146,7 @@ describe('fromLegacyTransactionInstruction', () => {
const data = new Uint8Array([70, 80, 90]);

const instruction = new TransactionInstruction({
data: Buffer.from(data),
data: toLegacyByteArrayAppropriateForPlatform(data),
keys,
programId: new PublicKey(programId),
});
Expand Down Expand Up @@ -170,7 +180,7 @@ describe('fromLegacyTransactionInstruction', () => {
];

const instruction = new TransactionInstruction({
data: Buffer.from(new Uint8Array()),
data: toLegacyByteArrayAppropriateForPlatform(new Uint8Array()),
keys,
programId: new PublicKey(programId),
});
Expand Down
4 changes: 4 additions & 0 deletions packages/compat/src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare const __BROWSER__: boolean;
declare const __DEV__: boolean;
declare const __NODEJS__: boolean;
declare const __REACTNATIVE__: boolean;

0 comments on commit 8111c88

Please sign in to comment.