<commit_before>import 'rxjs'
import { make, query, querySync } from '../../src/graph';
import { available } from '../../src/cache/strategies';
import Node from './Node'

const makeTestGraph = () => {
  const master = Node({
    id: 'master',
    params: { token: 'token' },
    fetch: () => Promise.resolve({ bar: { foo: 'foo' } })
  });
  const slave = Node({
    id: 'slave',
    strategy: available,
    dependencies: {
      master: { query: master }
    },
    fetch: ({ master: { bar } }) => Promise.resolve(bar)
  })
  const a = Node({
    id: 'a',
    strategy: available,
    fetch: () => Promise.resolve('a')
  })
  const b = Node({
    id: 'b',
    strategy: available,
    fetch: () => Promise.resolve('b')
  })

  return make({ ...master, ...slave, ...a, ...b });
}

describe('queriesSync', () => {

  it('should return complete structure even when no value is available', () => {
    const graph = makeTestGraph()

    const master = querySync(graph, ['master'], { token: 'token' })
    const slave = querySync(graph, ['slave'], { token: 'token' })
    const product = querySync(graph, ['a', 'b'], { token: 'token' })

    expect(master).toEqual({ data: { master: { loading: false } }, loading: false })
    expect(slave).toEqual({ data: { slave: { loading: true } }, loading: true })
    expect(product).toEqual({ data: { a: { loading: false }, b: { loading: false } }, loading: false })
  });

  it('should return current value', () => {
    const graph = makeTestGraph()

    query(graph, ['master'], { token: 'token' })
    query(graph, ['slave'], { token: 'token' })
    query(graph, ['a', 'b'], { token: 'token' })

    return new Promise((resolve, reject) => setTimeout(() => {
      const master = querySync(graph, ['master'], { token: 'token' })
      const slave = querySync(graph, ['slave'], { token: 'token' })
      const product = querySync(graph, ['a', 'b'], { token: 'token' })
<commit_msg>add test for "loading: true" field when queries are loading<commit_after>import 'rxjs'
import { make, query, querySync } from '../../src/graph';
import { available } from '../../src/cache/strategies';
import Node from './Node'

const makeTestGraph = () => {
  const master = Node({
    id: 'master',
    params: { token: 'token' },
    fetch: () => Promise.resolve({ bar: { foo: 'foo' } })
  });
  const slave = Node({
    id: 'slave',
    strategy: available,
    dependencies: {
      master: { query: master }
    },
    fetch: ({ master: { bar } }) => Promise.resolve(bar)
  })
  const a = Node({
    id: 'a',
    strategy: available,
    fetch: () => Promise.resolve('a')
  })
  const b = Node({
    id: 'b',
    strategy: available,
    fetch: () => Promise.resolve('b')
  })

  return make({ ...master, ...slave, ...a, ...b });
}

describe('queriesSync', () => {

  it('should return complete structure even when no value is available', () => {
    const graph = makeTestGraph()

    const master = querySync(graph, ['master'], { token: 'token' })
    const slave = querySync(graph, ['slave'], { token: 'token' })
    const product = querySync(graph, ['a', 'b'], { token: 'token' })

    expect(master).toEqual({ data: { master: { loading: false } }, loading: false })
    expect(slave).toEqual({ data: { slave: { loading: true } }, loading: true })
    expect(product).toEqual({ data: { a: { loading: false }, b: { loading: false } }, loading: false })
  });

  it('should return "loading: true" when queries are loading', () => {
    const graph = makeTestGraph()

    query(graph, ['master'], { token: 'token' })
    query(graph, ['slave'], { token: 'token' })
    query(graph, ['a', 'b'], { token: 'token' })

    const master = querySync(graph, ['master'], { token: 'token' })
    const slave = querySync(graph, ['slave'], { token: 'token' })
    const product = querySync(graph, ['a', 'b'], { token: 'token' })

    expect(master).toEqual({ data: { master: { loading: true } }, loading: true })
    expect(slave).toEqual({ data: { slave: { loading: true } }, loading: true })
    expect(product).toEqual({ data: { a: { loading: true }, b: { loading: true } }, loading: true })
  });

  it('should return current value', () => {
    const graph = makeTestGraph()

    query(graph, ['master'], { token: 'token' })
    query(graph, ['slave'], { token: 'token' })
    query(graph, ['a', 'b'], { token: 'token' })

    return new Promise((resolve, reject) => setTimeout(() => {
      const master = querySync(graph, ['master'], { token: 'token' })
      const slave = querySync(graph, ['slave'], { token: 'token' })
      const product = querySync(graph, ['a', 'b'], { token: 'token' })

{'fraction_non_alphanumeric': 0.209534127843987, 'fraction_numerical': 0.0, 'mean_word_length': 3.238751147842057, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4200718', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1428, 'n_words': 418}